-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathconfig_ssh.go
More file actions
64 lines (53 loc) · 2.06 KB
/
config_ssh.go
File metadata and controls
64 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package evergreen
import (
"context"
"github.com/mongodb/anser/bsonutil"
"github.com/mongodb/grip"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
type SSHConfig struct {
// TaskHostKey is the key pair used for SSHing onto task hosts.
TaskHostKey SSHKeyPair `bson:"task_host_key" json:"task_host_key" yaml:"task_host_key"`
// SpawnHostKey is the key pair used for SSHing onto spawn hosts.
SpawnHostKey SSHKeyPair `bson:"spawn_host_key" json:"spawn_host_key" yaml:"spawn_host_key"`
}
type SSHKeyPair struct {
// Name corresponds to the name of the public key for this key pair.
// Public keys in EC2 are referred to by names: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html
Name string `yaml:"name" bson:"name" json:"name"`
// SecretARN corresponds to a secret in AWS Secrets Manager that contains the private key for this key pair.
// This variable does not need to be a secret because it only contains the ARN that points to the secret.
SecretARN string `yaml:"secret_arn" bson:"secret_arn" json:"secret_arn"`
}
func (c *SSHConfig) SectionId() string { return "ssh" }
func (c *SSHConfig) Get(ctx context.Context) error {
return getConfigSection(ctx, c)
}
var (
taskHostKeyKey = bsonutil.MustHaveTag(SSHConfig{}, "TaskHostKey")
spawnHostKeyKey = bsonutil.MustHaveTag(SSHConfig{}, "SpawnHostKey")
)
func (c *SSHConfig) Set(ctx context.Context) error {
return errors.Wrapf(setConfigSection(ctx, c.SectionId(), bson.M{
"$set": bson.M{
taskHostKeyKey: c.TaskHostKey,
spawnHostKeyKey: c.SpawnHostKey,
}}), "updating config section '%s'", c.SectionId(),
)
}
func (c *SSHConfig) ValidateAndDefault() error {
catcher := grip.NewBasicCatcher()
catcher.Wrap(c.TaskHostKey.validate(), "validating task host key")
catcher.Wrap(c.SpawnHostKey.validate(), "validating spawn host key")
return catcher.Resolve()
}
func (c *SSHKeyPair) validate() error {
if c.Name != "" && c.SecretARN == "" {
return errors.New("secret ARN must be set")
}
if c.Name == "" && c.SecretARN != "" {
return errors.New("key name must be set")
}
return nil
}