-
Notifications
You must be signed in to change notification settings - Fork 43
feat(BRE2-1051): ssh certs uptake #452
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
patelspratik
wants to merge
1
commit into
main
Choose a base branch
from
feat/ssh-certs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Package mintcert implements the `brev mint-cert` command, which mints a | ||
| // short-lived SSH certificate for an environment and writes it (with its | ||
| // backing ephemeral keypair) to disk. It is invoked by the ssh config's | ||
| // Match exec hook, generated by `brev refresh`. | ||
| package mintcert | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| devplanev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1" | ||
| "connectrpc.com/connect" | ||
| "github.com/spf13/afero" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/brevdev/brev-cli/pkg/cmd/register" | ||
| "github.com/brevdev/brev-cli/pkg/config" | ||
| breverrors "github.com/brevdev/brev-cli/pkg/errors" | ||
| "github.com/brevdev/brev-cli/pkg/externalnode" | ||
| "github.com/brevdev/brev-cli/pkg/sshcert" | ||
| ) | ||
|
|
||
| const timeout = 15 * time.Second | ||
|
|
||
| type Store interface { | ||
| GetAccessToken() (string, error) | ||
| } | ||
|
|
||
| type CertIssuer interface { | ||
| Issue(ctx context.Context, req certIssueRequest) (string, error) | ||
| } | ||
|
|
||
| type certIssueRequest struct { | ||
| EnvironmentID string | ||
| PortID string | ||
| LinuxUser string | ||
| PublicKey string | ||
| } | ||
|
|
||
| type environmentCertClient interface { | ||
| IssueEnvironmentSSHCertificate(ctx context.Context, req *connect.Request[devplanev1.IssueEnvironmentSSHCertificateRequest]) (*connect.Response[devplanev1.IssueEnvironmentSSHCertificateResponse], error) | ||
| } | ||
|
|
||
| type rpcCertIssuer struct { | ||
| client environmentCertClient | ||
| } | ||
|
|
||
| func (r rpcCertIssuer) Issue(ctx context.Context, req certIssueRequest) (string, error) { | ||
| res, err := r.client.IssueEnvironmentSSHCertificate(ctx, connect.NewRequest(&devplanev1.IssueEnvironmentSSHCertificateRequest{ | ||
| EnvironmentId: req.EnvironmentID, | ||
| LinuxUser: req.LinuxUser, | ||
| PortId: req.PortID, | ||
| PublicKey: req.PublicKey, | ||
| })) | ||
| if err != nil { | ||
| return "", breverrors.WrapAndTrace(err) | ||
| } | ||
| return res.Msg.GetCertificate(), nil | ||
| } | ||
|
|
||
| func NewCmdMintCert(store Store) *cobra.Command { | ||
| var ( | ||
| env string | ||
| port string | ||
| user string | ||
| outKey string | ||
| ) | ||
| cmd := &cobra.Command{ | ||
| Use: "mint-cert", | ||
| Short: "Mint a short-lived SSH certificate for an environment", | ||
| Args: cobra.NoArgs, | ||
| Hidden: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return runMintCert(store, mintCertRequest{ | ||
| EnvironmentID: env, | ||
| PortID: port, | ||
| LinuxUser: user, | ||
| OutKey: outKey, | ||
| }) | ||
| }, | ||
| } | ||
| cmd.Flags().StringVar(&env, "env", "", "environment ID to mint a certificate for") | ||
| cmd.Flags().StringVar(&port, "port", "", "network-member port ID for the SSH access") | ||
| cmd.Flags().StringVar(&user, "linux-user", "", "Linux user for the certificate principal") | ||
| cmd.Flags().StringVar(&outKey, "out-key", "", "private-key path (certificate goes to <path>-cert.pub)") | ||
| _ = cmd.MarkFlagRequired("env") | ||
| _ = cmd.MarkFlagRequired("port") | ||
| _ = cmd.MarkFlagRequired("linux-user") | ||
| _ = cmd.MarkFlagRequired("out-key") | ||
| return cmd | ||
| } | ||
|
|
||
| type mintCertRequest struct { | ||
| EnvironmentID string | ||
| PortID string | ||
| LinuxUser string | ||
| OutKey string | ||
| } | ||
|
|
||
| func runMintCert(store Store, req mintCertRequest) error { | ||
| return runMintCertWith(store, afero.NewOsFs(), newCertIssuer(store, config.GlobalConfig.GetBrevPublicAPIURL()), req) | ||
| } | ||
|
|
||
| func runMintCertWith(store Store, fs afero.Fs, issuer CertIssuer, req mintCertRequest) error { | ||
| token, err := store.GetAccessToken() | ||
| if err != nil || token == "" { | ||
| _, _ = fmt.Fprintln(os.Stderr, "brev: not logged in. Run `brev login` and retry.") | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| return fmt.Errorf("not logged in") | ||
| } | ||
| _ = token | ||
| certPath := req.OutKey + "-cert.pub" | ||
| if ok, err := sshcert.HasValidCertAt(fs, certPath, time.Now(), sshcert.DefaultRenewalMargin); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "brev: failed to check cached cert: %v\n", err) | ||
| return breverrors.WrapAndTrace(err) | ||
| } else if ok { | ||
| return nil | ||
| } | ||
| privKeyPEM, pubKeyOpenSSH, err := sshcert.GenerateKeyPair() | ||
| if err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "brev: failed to generate keypair: %v\n", err) | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
| defer cancel() | ||
| cert, err := issuer.Issue(ctx, certIssueRequest{ | ||
| EnvironmentID: req.EnvironmentID, | ||
| PortID: req.PortID, | ||
| LinuxUser: req.LinuxUser, | ||
| PublicKey: pubKeyOpenSSH, | ||
| }) | ||
| if err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "brev: could not issue ssh certificate: %v\n", err) | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| if err := sshcert.WriteFiles(fs, req.OutKey, certPath, privKeyPEM, cert); err != nil { | ||
| _, _ = fmt.Fprintf(os.Stderr, "brev: failed to write cert files: %v\n", err) | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func newCertIssuer(provider externalnode.TokenProvider, baseURL string) CertIssuer { | ||
| return rpcCertIssuer{client: register.NewEnvironmentServiceClient(provider, baseURL)} | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep this vague (similar to ssh, which is "the name or ID") so that we could possibly support external nodes with the same command?