Repository navigation
feat(discovery): opt-in ssh_access exposes discovery scope as a dynamic ssh-proxy #157
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a8a883a
chore(deps): bump grpc 1.83.1 and x/crypto 0.56.0 to clear govulncheck
mayankpande88 7723dc5
ci: golangci-lint v2.13.2 for go 1.26; fix its new findings
mayankpande88 7ddd3ae
feat(discovery): opt-in ssh_access exposes discovery scope as a dynam…
mayankpande88 2ac76f2
fix: nil-safe Verifier.Enabled; drop empty scope entries from []strin…
mayankpande88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package discovery | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "nudgebee/forager/pkg/proxy" | ||
| sshproxy "nudgebee/forager/pkg/proxy/ssh" | ||
| ) | ||
|
|
||
| // SSHAccessSuffix is appended to a discovery datasource ID to form the ID of | ||
| // its SSH access sibling. | ||
| const SSHAccessSuffix = ":ssh" | ||
|
|
||
| // SSHAccessEnabled reports whether a discovery datasource config opts into | ||
| // exposing its hosts for ad-hoc commands. | ||
| func SSHAccessEnabled(cfg map[string]any) bool { | ||
| v, _ := cfg["ssh_access"].(bool) | ||
| return v | ||
| } | ||
|
mayankpande88 marked this conversation as resolved.
|
||
|
|
||
| // SSHAccessConfig derives the ssh-proxy config for the SSH access sibling of | ||
| // a discovery datasource. | ||
| // | ||
| // Discovery credentials are granted for signed content packs. ssh_access lets | ||
| // the same credentials run arbitrary commands, so it is held to a stricter bar | ||
| // than inventory: the scope must be explicit (an empty allowed_cidrs means | ||
| // "anywhere" to discovery, which is not acceptable for a shell), host keys | ||
| // must be verified, and requests must be signed. | ||
| func SSHAccessConfig(cfg map[string]any, signingEnabled bool) (map[string]any, error) { | ||
| if !signingEnabled { | ||
| return nil, errors.New("ssh_access requires signing_public_key so commands are verified") | ||
| } | ||
|
mayankpande88 marked this conversation as resolved.
|
||
|
|
||
| knownHosts, _ := cfg["known_hosts_file"].(string) | ||
| if knownHosts == "" { | ||
| return nil, errors.New("ssh_access requires known_hosts_file so host keys are verified") | ||
| } | ||
|
|
||
| cidrs := stringSlice(cfg["allowed_cidrs"]) | ||
| if len(cidrs) == 0 { | ||
| return nil, errors.New("ssh_access requires a non-empty allowed_cidrs scope") | ||
| } | ||
|
|
||
| out := map[string]any{ | ||
| "host": "", // dynamic mode: the target comes with each request | ||
| "allowed_hosts": cidrs, | ||
| "known_hosts": knownHosts, | ||
| } | ||
| if port, ok := cfg["port"]; ok { | ||
| out["port"] = port | ||
| } | ||
| if n, ok := cfg["max_output_bytes"]; ok { | ||
| out["max_output_bytes"] = n | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // SSHAccessSibling builds and configures the ssh-proxy that exposes a | ||
| // discovery datasource's hosts for ad-hoc commands. The returned entry | ||
| // registers it as its own datasource, reported to the server as type ssh | ||
| // for it, leaving the discovery datasource's type untouched. | ||
| func SSHAccessSibling(discovery proxy.DatasourceEntry, cfg map[string]any, creds map[string]string, signingEnabled bool, logger *slog.Logger) (proxy.DatasourceEntry, proxy.Proxy, error) { | ||
| sshCfg, err := SSHAccessConfig(cfg, signingEnabled) | ||
| if err != nil { | ||
| return proxy.DatasourceEntry{}, nil, err | ||
| } | ||
|
|
||
| id := discovery.ID + SSHAccessSuffix | ||
| p := sshproxy.New(logger.With("datasource", id)) | ||
| if err := p.Configure(sshCfg, creds); err != nil { | ||
| return proxy.DatasourceEntry{}, nil, fmt.Errorf("configuring ssh access: %w", err) | ||
| } | ||
|
|
||
| entry := proxy.DatasourceEntry{ | ||
| ID: id, | ||
| Type: "ssh", | ||
| ProxyType: p.Type(), | ||
| Name: discovery.Name + "-ssh", | ||
| CredentialSource: discovery.CredentialSource, | ||
| CredentialRef: discovery.CredentialRef, | ||
| } | ||
| return entry, p, nil | ||
| } | ||
|
|
||
| // stringSlice accepts both []string (local config) and []any (JSON-decoded | ||
| // cloud push) forms, dropping empty entries from either. | ||
| func stringSlice(v any) []string { | ||
| switch s := v.(type) { | ||
| case []string: | ||
| out := make([]string, 0, len(s)) | ||
| for _, e := range s { | ||
| if e != "" { | ||
| out = append(out, e) | ||
| } | ||
| } | ||
| return out | ||
| case []any: | ||
| out := make([]string, 0, len(s)) | ||
| for _, e := range s { | ||
| if str, ok := e.(string); ok && str != "" { | ||
| out = append(out, str) | ||
| } | ||
| } | ||
| return out | ||
| default: | ||
| return nil | ||
| } | ||
|
blue4209211 marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.