Skip to content

Commit

Permalink
Merge pull request #3 from ikifar2012/dev
Browse files Browse the repository at this point in the history
Fix Permission issues and refactor
  • Loading branch information
ikifar2012 authored Oct 24, 2024
2 parents e4157a5 + 4dc827c commit d3c561d
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 31 deletions.
46 changes: 46 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/powershell
{
"name": "PowerShell",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/powershell:lts-debian-11",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": "true",
"username": "vscode",
"upgradePackages": "false",
"nonFreePackages": "true"
},
"ghcr.io/devcontainers/features/powershell:1": {
"version": "latest"
}
},

"postCreateCommand": "sudo chsh vscode -s \"$(which pwsh)\"",

// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.defaultProfile.linux": "pwsh"
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-vscode.powershell",
"github.vscode-github-actions",
"VisualStudioExptTeam.vscodeintellicode",
"GitHub.copilot",
"GitHub.copilot-chat"
]
}
}

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
6 changes: 6 additions & 0 deletions .github/removate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"github>ikifar2012/.github"
]
}
39 changes: 39 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Publish PowerShell Module

on:
release:
types: ["published"]

jobs:
publish-to-gallery:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Publish
env:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
shell: pwsh
run: |
$modulePath = Join-Path $env:GITHUB_WORKSPACE "ps-copyid"
if (-not (Test-Path $modulePath)) {
throw "Module path not found: $modulePath"
}
try {
Write-Host "Publishing module from: $modulePath"
Publish-Module -Path $modulePath -NuGetApiKey $env:NUGET_KEY -Repository PSGallery -ErrorAction Stop
# Verify publish was successful
$moduleInfo = Test-ModuleManifest -Path (Join-Path $modulePath "ps-copyid.psd1")
$publishedModule = Find-Module -Name $moduleInfo.Name -RequiredVersion $moduleInfo.Version
if ($publishedModule) {
Write-Host "Successfully published $($moduleInfo.Name) version $($moduleInfo.Version)"
} else {
throw "Module verification failed after publish"
}
}
catch {
throw "Failed to publish module: $_"
}
31 changes: 0 additions & 31 deletions ps-copyid.psm1

This file was deleted.

File renamed without changes.
69 changes: 69 additions & 0 deletions ps-copyid/ps-copyid.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function Copy-ID {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[String]$hostname,
[Parameter(Mandatory=$false)]
[String]$id
)

# If no key specified, find existing keys
if (-not $id) {
$sshdir = Join-Path $HOME '.ssh'
Write-Host "Testing Keys..."

# Check for keys in preference order
$keyTypes = @(
@{ Type = "ED25519"; Path = Join-Path $sshdir 'id_ed25519.pub' },
@{ Type = "ECDSA"; Path = Join-Path $sshdir 'id_ecdsa.pub' },
@{ Type = "RSA"; Path = Join-Path $sshdir 'id_rsa.pub' }
)

foreach ($key in $keyTypes) {
if (Test-Path -Path $key.Path) {
Write-Host "Found $($key.Type) Key, Installing..."
$id = $key.Path
break
}
}

if (-not $id) {
Write-Warning "No keys found"
$id = Read-Host -Prompt "Please manually enter the path to your public key"
}
}
else {
Write-Host "Installing Key..."
}

# Verify key exists
if (-not (Test-Path -Path $id)) {
throw "Could not find public key file: $id"
}

# Verify key format
$keyContent = Get-Content $id
if (-not ($keyContent -match '^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521)\s+[A-Za-z0-9+/]+[=]{0,3}\s+.*$')) {
throw "Invalid SSH public key format in file: $id"
}

# The critical part: Remote commands to handle SSH directory and permissions
$remoteCommands = @'
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
cat >> ~/.ssh/authorized_keys
'@

Get-Content $id | ssh $hostname $remoteCommands
# Check for success
if ($?) {
Write-Host "Key Installed Successfully"
}
else {
Write-Warning "Key Installation Failed"
}
}

Export-ModuleMember -Function Copy-ID

0 comments on commit d3c561d

Please sign in to comment.