Skip to content

Commit 4d225ea

Browse files
authored
VPC key create and delete operations added (#1113)
1 parent 27fdc25 commit 4d225ea

File tree

5 files changed

+283
-4
lines changed

5 files changed

+283
-4
lines changed

cmd/capibmadm/cmd/vpc/key/create.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package key
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
24+
"github.com/spf13/cobra"
25+
"golang.org/x/crypto/ssh"
26+
27+
"github.com/IBM/vpc-go-sdk/vpcv1"
28+
29+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
30+
31+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
32+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/vpc"
33+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
34+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
35+
)
36+
37+
type keyCreateOptions struct {
38+
name string
39+
publicKey string
40+
resourceGroupName string
41+
}
42+
43+
// CreateCommand vpc key create command.
44+
func CreateCommand() *cobra.Command {
45+
cmd := &cobra.Command{
46+
Use: "create",
47+
Short: "Create VPC key",
48+
Example: `
49+
# Create key in VPC
50+
export IBMCLOUD_API_KEY=<api-key>
51+
capibmadm vpc key create --name <key-name> --region <region> --public-key "<public-key-string>"
52+
Using file-path to SSH key : capibmadm vpc key create --name <key-name> --region <region> --key-path <path/to/ssh/key>
53+
`,
54+
}
55+
56+
options.AddCommonFlags(cmd)
57+
var keyCreateOption keyCreateOptions
58+
var filePath string
59+
cmd.Flags().StringVar(&keyCreateOption.name, "name", keyCreateOption.name, "Key Name")
60+
cmd.Flags().StringVar(&filePath, "key-path", "", "The absolute path to the SSH key file.")
61+
cmd.Flags().StringVar(&keyCreateOption.publicKey, "public-key", keyCreateOption.publicKey, "Public Key")
62+
_ = cmd.MarkFlagRequired("name")
63+
// TODO: Flag validation is handled in PreRunE until the support for MarkFlagsMutuallyExclusiveAndRequired is available.
64+
// Related issue: https://github.com/spf13/cobra/issues/1216
65+
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
66+
if (keyCreateOption.publicKey == "") == (filePath == "") {
67+
return fmt.Errorf("the required flags either key-path of SSH key or the public-key within double quotation marks is not found")
68+
}
69+
return nil
70+
}
71+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
72+
if filePath != "" {
73+
sshKey, err := os.ReadFile(filePath) // #nosec
74+
if err != nil {
75+
return fmt.Errorf("error while reading the SSH key from path. %w", err)
76+
}
77+
keyCreateOption.publicKey = string(sshKey)
78+
}
79+
80+
if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(keyCreateOption.publicKey)); err != nil {
81+
return fmt.Errorf("the provided SSH key is invalid. %w ", err)
82+
}
83+
return createKey(cmd.Context(), keyCreateOption)
84+
}
85+
return cmd
86+
}
87+
88+
func createKey(ctx context.Context, keyCreateOption keyCreateOptions) error {
89+
log := logf.Log
90+
vpcClient, err := vpc.NewV1Client(options.GlobalOptions.VPCRegion)
91+
if err != nil {
92+
return err
93+
}
94+
95+
accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
96+
if err != nil {
97+
return err
98+
}
99+
100+
options := &vpcv1.CreateKeyOptions{}
101+
102+
options.SetName(keyCreateOption.name)
103+
options.SetPublicKey(keyCreateOption.publicKey)
104+
105+
if keyCreateOption.resourceGroupName != "" {
106+
resourceGroupID, err := utils.GetResourceGroupID(ctx, keyCreateOption.resourceGroupName, accountID)
107+
if err != nil {
108+
return err
109+
}
110+
resourceGroup := &vpcv1.ResourceGroupIdentity{
111+
ID: &resourceGroupID,
112+
}
113+
options.SetResourceGroup(resourceGroup)
114+
}
115+
116+
key, _, err := vpcClient.CreateKey(options)
117+
if err != nil {
118+
return err
119+
}
120+
log.Info("SSH Key created successfully,", "key-name", *key.Name)
121+
return nil
122+
}

cmd/capibmadm/cmd/vpc/key/delete.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package key
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
"github.com/IBM/vpc-go-sdk/vpcv1"
25+
26+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
27+
28+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/vpc"
29+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
30+
)
31+
32+
type keyDeleteOptions struct {
33+
name string
34+
}
35+
36+
// DeleteCommand vpc key delete command.
37+
func DeleteCommand() *cobra.Command {
38+
cmd := &cobra.Command{
39+
Use: "delete",
40+
Short: "Delete VPC key",
41+
Example: `
42+
# Delete key in VPC
43+
export IBMCLOUD_API_KEY=<api-key>
44+
capibmadm vpc key delete --name <key-name> --region <region>`,
45+
}
46+
47+
options.AddCommonFlags(cmd)
48+
var keyDeleteOption keyDeleteOptions
49+
cmd.Flags().StringVar(&keyDeleteOption.name, "name", keyDeleteOption.name, "Key Name")
50+
_ = cmd.MarkFlagRequired("name")
51+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
52+
return deleteKey(keyDeleteOption)
53+
}
54+
55+
return cmd
56+
}
57+
58+
func deleteKey(keyDeleteOption keyDeleteOptions) error {
59+
log := logf.Log
60+
vpcClient, err := vpc.NewV1Client(options.GlobalOptions.VPCRegion)
61+
if err != nil {
62+
return err
63+
}
64+
65+
listKeysOptions := &vpcv1.ListKeysOptions{}
66+
pager, err := vpcClient.NewKeysPager(listKeysOptions)
67+
if err != nil {
68+
return err
69+
}
70+
71+
var allResults []vpcv1.Key
72+
for pager.HasNext() {
73+
nextPage, err := pager.GetNext()
74+
if err != nil {
75+
return err
76+
}
77+
allResults = append(allResults, nextPage...)
78+
}
79+
80+
var keyID string
81+
for _, key := range allResults {
82+
if *key.Name == keyDeleteOption.name {
83+
keyID = *key.ID
84+
break
85+
}
86+
}
87+
88+
if keyID == "" {
89+
return fmt.Errorf("specified key %s could not be found", keyDeleteOption.name)
90+
}
91+
92+
options := &vpcv1.DeleteKeyOptions{}
93+
options.SetID(keyID)
94+
95+
if _, err := vpcClient.DeleteKey(options); err != nil {
96+
return err
97+
}
98+
log.Info("SSH Key deleted successfully,", "key-name", keyDeleteOption.name)
99+
return nil
100+
}

cmd/capibmadm/cmd/vpc/key/key.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func Commands() *cobra.Command {
2929
}
3030

3131
cmd.AddCommand(ListCommand())
32-
32+
cmd.AddCommand(CreateCommand())
33+
cmd.AddCommand(DeleteCommand())
3334
return cmd
3435
}

docs/book/src/topics/capibmadm/vpc/index.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33

44
## 1. VPC commands
5-
- [image](./image.md)
6-
- [list](/topics/capibmadm/vpc/image.html#1-capibmadm-vpc-image-list)
5+
76
- [key](./key.md)
87
- [list](/topics/capibmadm/vpc/key.html#1-capibmadm-vpc-key-list)
8+
- [create](/topics/capibmadm/vpc/key.html#2-capibmadm-vpc-key-create)
9+
- [delete](/topics/capibmadm/vpc/key.html#3-capibmadm-vpc-key-delete)
10+
11+
- [image](./image.md)
12+
- [list](/topics/capibmadm/vpc/image.html#1-capibmadm-vpc-image-list)

docs/book/src/topics/capibmadm/vpc/key.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,56 @@ IBMCLOUD_API_KEY: IBM Cloud API key.
1717
```shell
1818
export IBMCLOUD_API_KEY=<api-key>
1919
capibmadm vpc key list --region <region> --resource-group-name <resource-group>
20-
```
20+
```
21+
22+
### 2. capibmadm vpc key create
23+
24+
#### Usage:
25+
Create a key in the VPC environment.
26+
27+
#### Environmental Variable:
28+
IBMCLOUD_API_KEY: IBM Cloud API key.
29+
30+
#### Arguments:
31+
32+
--name: The name of the key.
33+
34+
--resource-group-name: VPC resource group name.
35+
36+
--region: VPC region.
37+
38+
Either of the arguments need to be provided:
39+
40+
--public-key: Public key string within a double quotation marks. For example, "ssh-rsa AAA... ".
41+
42+
--key-path: The absolute path to the SSH key file.
43+
44+
45+
#### Example:
46+
```shell
47+
export IBMCLOUD_API_KEY=<api-key>
48+
49+
capibmadm vpc key create --name <key-name> --region <region> --public-key "<public-key-string>"
50+
51+
capibmadm vpc key create --name <key-name> --region <region> --key-path <path/to/ssh/key>
52+
```
53+
54+
### 3. capibmadm vpc key delete
55+
56+
#### Usage:
57+
Delete a key in the VPC environment.
58+
59+
#### Environmental Variable:
60+
IBMCLOUD_API_KEY: IBM Cloud API key.
61+
62+
#### Arguments:
63+
64+
--name: The name of the key.
65+
66+
--region: VPC region.
67+
68+
#### Example:
69+
```shell
70+
export IBMCLOUD_API_KEY=<api-key>
71+
capibmadm vpc key delete --name <key-name> --region <region>
72+
```

0 commit comments

Comments
 (0)