Skip to content

Commit e83b4fe

Browse files
Merge pull request #27 from ramchandarkg/enhancement/validate-ssh-key
[Enhancement]Validating SSH Key
2 parents 08f4469 + 197d8dd commit e83b4fe

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

lib/cmd_add.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ source "$(dirname "${BASH_SOURCE[0]}")/common.sh"
44
source "$(dirname "${BASH_SOURCE[0]}")/profile.sh"
55
source "$(dirname "${BASH_SOURCE[0]}")/ssh_writer.sh"
66

7+
validate_ssh_key() {
8+
local key_path="$1"
9+
if [[ ! -f "$key_path" ]]; then
10+
log_error "Error: SSH key file '$key_path' does not exist."
11+
return 1
12+
fi
13+
local perms
14+
perms=$(stat -c "%a" "$key_path" 2>/dev/null || stat -f "%Lp" "$key_path")
15+
if [[ "$perms" != "600" ]]; then
16+
log_error "Error: SSH key file '$key_path' must have 600 permissions."
17+
return 1
18+
fi
19+
return 0
20+
}
21+
722
cmd_add() {
823
local name="$1"
924

@@ -15,8 +30,13 @@ cmd_add() {
1530
echo "Creating profile: ${name}"
1631

1732
read -p "SSH key path (~/.ssh/): " ssh_key
33+
ssh_key="${ssh_key/#\~/$HOME}"
1834
ssh_key="${ssh_key:-$HOME/.ssh/id_rsa}"
1935

36+
if ! validate_ssh_key "$ssh_key"; then
37+
exit 1
38+
fi
39+
2040
read -p "Git name: " git_name
2141
read -p "Git email: " git_email
2242
read -p "GitHub username: " github_user

0 commit comments

Comments
 (0)