Skip to content

Add support for ignoring keys #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ Usage: sshb0t <command>

Flags:

--url GitHub Enterprise URL (default: https://github.com)
--user GitHub usernames for which to fetch keys (default: [])
-d enable debug logging (default: false)
--ignore ignore SSH keys that match (default: [])
--interval update interval (ex. 5ms, 10s, 1m, 3h) (default: 30s)
--keyfile file to update the authorized_keys (default: /home/jessie/.ssh/authorized_keys)
--once run once and exit, do not run as a daemon (default: false)
--url GitHub Enterprise URL (default: https://github.com)
--user GitHub usernames for which to fetch keys (default: [])

Commands:

Expand Down
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
authorizedKeysFile string
enturl string
users stringSlice
ignored stringSlice

interval time.Duration
once bool
Expand Down Expand Up @@ -72,6 +73,7 @@ func main() {
p.FlagSet.StringVar(&authorizedKeysFile, "keyfile", filepath.Join(home, defaultSSHAuthorizedKeysFile), "file to update the authorized_keys")
p.FlagSet.StringVar(&enturl, "url", "https://github.com", "GitHub Enterprise URL")
p.FlagSet.Var(&users, "user", "GitHub usernames for which to fetch keys")
p.FlagSet.Var(&ignored, "ignore", "ignore SSH keys that match")

p.FlagSet.DurationVar(&interval, "interval", 30*time.Second, "update interval (ex. 5ms, 10s, 1m, 3h)")
p.FlagSet.BoolVar(&once, "once", false, "run once and exit, do not run as a daemon")
Expand Down Expand Up @@ -157,8 +159,14 @@ func run() {
logrus.Fatalf("Reading response body from %s for user %s failed: %v", uri, user, err)
continue
}
// append to keys variable with a new line
keys += string(b)

for _, key := range strings.Split(string(b), "\n") {
if isIgnored(key) {
continue
}
// append to keys variable with a new line
keys += key + "\n"
}
}

// update the authorized key file
Expand All @@ -169,6 +177,15 @@ func run() {
logrus.Info("Successfully updated keys")
}

func isIgnored(key string) bool {
for _, i := range ignored {
if strings.Contains(key, i) {
return true
}
}
return false
}

func getHomeDir() (string, error) {
home := os.Getenv(homeKey)
if home != "" {
Expand Down