Skip to content

Commit 33407c7

Browse files
authored
Merge pull request #1445 from ioito/hotfix/qx-aws-mfa
fix(aws): add aws mfa cli
2 parents 8bb4481 + 8d198ba commit 33407c7

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

pkg/multicloud/aws/mfa.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package aws
2+
3+
import "time"
4+
5+
type VirtualMFADevice struct {
6+
EnableDate time.Time
7+
SerialNumber string
8+
Base32StringSeed string
9+
QRCodePNG string
10+
User struct {
11+
UserName string
12+
Arn string
13+
UserId string
14+
CreateDate time.Time
15+
}
16+
}
17+
18+
func (cli *SAwsClient) GetVirtualMFADevices() ([]VirtualMFADevice, error) {
19+
ret := []VirtualMFADevice{}
20+
for {
21+
params := map[string]string{}
22+
part := struct {
23+
Marker string `xml:"Marker"`
24+
VirtualMFADevices []VirtualMFADevice `xml:"VirtualMFADevices>member"`
25+
}{}
26+
err := cli.iamRequest("ListVirtualMFADevices", params, &part)
27+
if err != nil {
28+
return nil, err
29+
}
30+
ret = append(ret, part.VirtualMFADevices...)
31+
if len(part.VirtualMFADevices) == 0 || len(part.Marker) == 0 {
32+
break
33+
}
34+
params["Marker"] = part.Marker
35+
}
36+
return ret, nil
37+
}
38+
39+
func (cli *SAwsClient) DeleteVirtualMFADevice(serialNumber, userName string) error {
40+
params := map[string]string{
41+
"SerialNumber": serialNumber,
42+
}
43+
if len(userName) > 0 {
44+
params["UserName"] = userName
45+
}
46+
return cli.iamRequest("DeactivateMFADevice", params, nil)
47+
}
48+
49+
func (cli *SAwsClient) CreateVirtualMFADevice(name string) (*VirtualMFADevice, error) {
50+
params := map[string]string{
51+
"VirtualMFADeviceName": name,
52+
}
53+
ret := struct {
54+
VirtualMFADevice VirtualMFADevice `xml:"VirtualMFADevice"`
55+
}{}
56+
err := cli.iamRequest("CreateVirtualMFADevice", params, &ret)
57+
if err != nil {
58+
return nil, err
59+
}
60+
return &ret.VirtualMFADevice, nil
61+
}
62+
63+
func (cli *SAwsClient) ResyncMFADevice(serialNumber, userName, code1, code2 string) error {
64+
params := map[string]string{
65+
"SerialNumber": serialNumber,
66+
"AuthenticationCode1": code1,
67+
"AuthenticationCode2": code2,
68+
"UserName": userName,
69+
}
70+
return cli.iamRequest("ResyncMFADevice", params, nil)
71+
}

pkg/multicloud/aws/shell/mfa.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2019 Yunion
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package shell
16+
17+
import (
18+
"yunion.io/x/pkg/util/shellutils"
19+
20+
"yunion.io/x/cloudmux/pkg/multicloud/aws"
21+
)
22+
23+
func init() {
24+
type VirtualMFADeviceListOptions struct {
25+
}
26+
27+
shellutils.R(&VirtualMFADeviceListOptions{}, "virtual-mfa-device-list", "List virtual mfa devices", func(cli *aws.SRegion, args *VirtualMFADeviceListOptions) error {
28+
devices, err := cli.GetClient().GetVirtualMFADevices()
29+
if err != nil {
30+
return err
31+
}
32+
printList(devices, 0, 0, 0, nil)
33+
return nil
34+
})
35+
36+
type VirtualMFADeviceDeleteOptions struct {
37+
SerialNumber string
38+
UserName string
39+
}
40+
41+
shellutils.R(&VirtualMFADeviceDeleteOptions{}, "virtual-mfa-device-deactivate", "Deactivate virtual mfa device", func(cli *aws.SRegion, args *VirtualMFADeviceDeleteOptions) error {
42+
return cli.GetClient().DeleteVirtualMFADevice(args.SerialNumber, args.UserName)
43+
})
44+
45+
type VirtualMFADeviceCreateOptions struct {
46+
NAME string
47+
}
48+
49+
shellutils.R(&VirtualMFADeviceCreateOptions{}, "virtual-mfa-device-create", "Create virtual mfa device", func(cli *aws.SRegion, args *VirtualMFADeviceCreateOptions) error {
50+
device, err := cli.GetClient().CreateVirtualMFADevice(args.NAME)
51+
if err != nil {
52+
return err
53+
}
54+
printObject(device)
55+
return nil
56+
})
57+
58+
type VirtualMFADeviceResyncOptions struct {
59+
SERIAL_NUMBER string
60+
USER string
61+
CODE1 string
62+
CODE2 string
63+
}
64+
65+
shellutils.R(&VirtualMFADeviceResyncOptions{}, "virtual-mfa-device-resync", "Resync virtual mfa device", func(cli *aws.SRegion, args *VirtualMFADeviceResyncOptions) error {
66+
return cli.GetClient().ResyncMFADevice(args.SERIAL_NUMBER, args.USER, args.CODE1, args.CODE2)
67+
})
68+
}

0 commit comments

Comments
 (0)