Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit d72fa00

Browse files
committed
Adds flag to use own key pair
1 parent dca5cb3 commit d72fa00

File tree

8 files changed

+80
-20
lines changed

8 files changed

+80
-20
lines changed

cmd/tarmak/cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ func init() {
105105
"override the current cluster set in the config",
106106
)
107107

108+
RootCmd.PersistentFlags().StringVar(
109+
&globalFlags.KeyPairName,
110+
"key-pair",
111+
"",
112+
"name of an existing key pair to use",
113+
)
114+
108115
if version == "dev" {
109116
RootCmd.PersistentFlags().BoolVar(
110117
&globalFlags.WingDevMode,

pkg/apis/tarmak/v1alpha1/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ type Flags struct {
127127
Version string // expose tarmak's build time version
128128

129129
WingDevMode bool // use a bundled wing version rather than a tagged release from GitHub
130+
131+
KeyPairName string // use an existing key pair
130132
}
131133

132134
// This contains the cluster specifc operation flags

pkg/tarmak/cluster/cluster.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,18 @@ func (c *Cluster) validateInstancePools() (result error) {
157157
}
158158

159159
// Verify cluster
160-
func (c *Cluster) Verify() (result error) {
161-
return c.VerifyInstancePools()
160+
func (c *Cluster) Verify() error {
161+
var result *multierror.Error
162+
163+
if err := c.VerifyInstancePools(); err != nil {
164+
result = multierror.Append(result, err)
165+
}
166+
167+
if err := c.Environment().Verify(); err != nil {
168+
result = multierror.Append(result, err)
169+
}
170+
171+
return result.ErrorOrNil()
162172
}
163173

164174
// Verify instance pools

pkg/tarmak/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,7 @@ func (c *Config) Project() string {
351351
func (c *Config) WingDevMode() bool {
352352
return c.flags.WingDevMode
353353
}
354+
355+
func (c *Config) KeyName() string {
356+
return c.flags.KeyPairName
357+
}

pkg/tarmak/environment/environment.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,13 @@ func (e *Environment) ValidateAdminCIDRs() (result error) {
301301
return result
302302
}
303303

304-
func (e *Environment) Verify() (result error) {
305-
return result
304+
func (e *Environment) Verify() error {
305+
var result *multierror.Error
306+
if err := e.Provider().Verify(); err != nil {
307+
result = multierror.Append(result, err)
308+
}
309+
310+
return result.ErrorOrNil()
306311
}
307312

308313
func (e *Environment) WingTunnel() interfaces.Tunnel {

pkg/tarmak/interfaces/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ type Config interface {
185185
Project() string
186186
WingDevMode() bool
187187
SetCurrentCluster(string) error
188+
KeyName() string
188189
}
189190

190191
type Packer interface {

pkg/tarmak/provider/amazon/amazon.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,11 @@ func (a *Amazon) readVaultToken() (string, error) {
317317
}
318318

319319
func (a *Amazon) Validate() error {
320-
var result error
321-
var err error
320+
var result *multierror.Error
322321

323322
// These checks only make sense with an environment given
324323
if a.tarmak.Environment() != nil {
325-
err = a.validateRemoteStateBucket()
324+
err := a.validateRemoteStateBucket()
326325
if err != nil {
327326
result = multierror.Append(result, err)
328327
}
@@ -337,26 +336,23 @@ func (a *Amazon) Validate() error {
337336
result = multierror.Append(result, err)
338337
}
339338

340-
err = a.validateAWSKeyPair()
341-
if err != nil {
342-
result = multierror.Append(result, err)
343-
}
344-
345339
}
346340

347-
err = a.validatePublicZone()
341+
err := a.validatePublicZone()
348342
if err != nil {
349343
result = multierror.Append(result, err)
350344
}
351345

352-
if result != nil {
353-
return result
354-
}
355-
return nil
356-
346+
return result.ErrorOrNil()
357347
}
358348

359-
func (a *Amazon) Verify() (result error) {
349+
func (a *Amazon) Verify() error {
350+
var result *multierror.Error
351+
352+
if err := a.verifyAWSKeyPair(); err != nil {
353+
result = multierror.Append(result, err)
354+
}
355+
360356
return result
361357
}
362358

pkg/tarmak/provider/amazon/key_pair.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,42 @@ func fingerprintAWSStyle(signer interface{}) (string, error) {
4040
}
4141
}
4242

43-
func (a *Amazon) validateAWSKeyPair() error {
43+
func (a *Amazon) getExisitingKeyPair(name string) (*ec2.KeyPairInfo, error) {
44+
svc, err := a.EC2()
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
keypairs, err := svc.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
50+
KeyNames: []*string{aws.String(name)},
51+
})
52+
if err != nil {
53+
return nil, fmt.Errorf("failed to get exisiting key pair: %v", err)
54+
}
55+
56+
if len(keypairs.KeyPairs) == 0 {
57+
return nil, fmt.Errorf("key pair '%s' does not exist", name)
58+
}
59+
if len(keypairs.KeyPairs) != 1 {
60+
return nil, fmt.Errorf("received an unexpected number of key pairs: %d", len(keypairs.KeyPairs))
61+
}
62+
63+
return keypairs.KeyPairs[0], nil
64+
}
65+
66+
func (a *Amazon) verifyAWSKeyPair() error {
67+
// if key pair has been given
68+
if name := a.tarmak.Config().KeyName(); name != "" {
69+
_, err := a.getExisitingKeyPair(name)
70+
if err != nil {
71+
return err
72+
}
73+
74+
a.conf.Amazon.KeyName = name
75+
76+
return nil
77+
}
78+
4479
svc, err := a.EC2()
4580
if err != nil {
4681
return err

0 commit comments

Comments
 (0)