Skip to content

Commit

Permalink
feat(box): add option to provide SSH agent (#462)
Browse files Browse the repository at this point in the history
## What this PR does / why we need it

This is needed in order to pass in a private SSH key from a Kubernetes
secret into a job that needs the box config.
  • Loading branch information
malept authored Feb 9, 2024
1 parent 435b5a8 commit 7275803
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 6 additions & 5 deletions pkg/box/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/agent"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -123,6 +124,8 @@ func EnsureBoxWithOptions(ctx context.Context, optFns ...LoadBoxOption) (*Config
// Always default to the min version being the version
// in this package.
MinVersion: &v,

Agent: sshhelper.GetSSHAgent(),
}

for _, f := range optFns {
Expand Down Expand Up @@ -161,7 +164,7 @@ func EnsureBoxWithOptions(ctx context.Context, optFns ...LoadBoxOption) (*Config

opts.log.WithField("reason", reason).Info("Refreshing box configuration")
// past the time interval, refresh the config
s.Config, err = downloadBox(ctx, s.StorageURL)
s.Config, err = downloadBox(ctx, opts.Agent, s.StorageURL)
if err != nil {
return nil, err
}
Expand All @@ -176,9 +179,7 @@ func EnsureBoxWithOptions(ctx context.Context, optFns ...LoadBoxOption) (*Config

// downloadBox downloads and parses a box config from a given repository
// URL.
func downloadBox(ctx context.Context, gitRepo string) (yaml.Node, error) {
a := sshhelper.GetSSHAgent()

func downloadBox(ctx context.Context, a agent.Agent, gitRepo string) (yaml.Node, error) {
//nolint:errcheck // Why: Best effort and not worth bringing logger here
_, err := sshhelper.LoadDefaultKey("github.com", a, &logrus.Logger{Out: io.Discard})
if err != nil {
Expand Down Expand Up @@ -247,7 +248,7 @@ func InitializeBox(ctx context.Context, _ []string) error {
return err
}

conf, err := downloadBox(ctx, gitRepo)
conf, err := downloadBox(ctx, sshhelper.GetSSHAgent(), gitRepo)
if err != nil {
return err
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/box/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package box

import "github.com/sirupsen/logrus"
import (
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/agent"
)

type LoadBoxOptions struct {
// MinVersion of a box configuration that is required for this
Expand All @@ -16,7 +19,10 @@ type LoadBoxOptions struct {
// Deprecated: Configure before running an application instead.
DefaultBoxSources []string

// log is the logger to use
// Agent is the SSH agent used when fetching the box git repository.
Agent agent.Agent

// log is the logger to use.
log logrus.FieldLogger
}

Expand All @@ -41,6 +47,15 @@ func WithDefaults(defaults []string) LoadBoxOption {
}
}

// WithAgent sets the SSH agent for fetching the box repository.
// If not specified, it creates a new one and uses the existing SSH
// config to load the github.com SSH key specified.
func WithAgent(sshAgent agent.Agent) LoadBoxOption {
return func(opts *LoadBoxOptions) {
opts.Agent = sshAgent
}
}

// WithLogger sets the logger to use when outputting to the user.
func WithLogger(log logrus.FieldLogger) LoadBoxOption {
return func(opts *LoadBoxOptions) {
Expand Down

0 comments on commit 7275803

Please sign in to comment.