forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypairs.go
156 lines (128 loc) · 4.31 KB
/
keypairs.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mockec2
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"
"k8s.io/kops/pkg/pki"
)
func (m *MockEC2) DescribeKeyPairsRequest(*ec2.DescribeKeyPairsInput) (*request.Request, *ec2.DescribeKeyPairsOutput) {
panic("MockEC2 DescribeKeyPairsRequest not implemented")
}
func (m *MockEC2) DescribeKeyPairsWithContext(aws.Context, *ec2.DescribeKeyPairsInput, ...request.Option) (*ec2.DescribeKeyPairsOutput, error) {
panic("Not implemented")
}
func (m *MockEC2) ImportKeyPairRequest(*ec2.ImportKeyPairInput) (*request.Request, *ec2.ImportKeyPairOutput) {
panic("MockEC2 ImportKeyPairRequest not implemented")
}
func (m *MockEC2) ImportKeyPairWithContext(aws.Context, *ec2.ImportKeyPairInput, ...request.Option) (*ec2.ImportKeyPairOutput, error) {
panic("Not implemented")
}
func (m *MockEC2) ImportKeyPair(request *ec2.ImportKeyPairInput) (*ec2.ImportKeyPairOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
glog.Infof("ImportKeyPair: %v", request)
fp, err := pki.ComputeAWSKeyFingerprint(string(request.PublicKeyMaterial))
if err != nil {
return nil, err
}
kp := &ec2.KeyPairInfo{
KeyFingerprint: aws.String(fp),
KeyName: request.KeyName,
}
if m.KeyPairs == nil {
m.KeyPairs = make(map[string]*ec2.KeyPairInfo)
}
m.KeyPairs[aws.StringValue(request.KeyName)] = kp
response := &ec2.ImportKeyPairOutput{
KeyFingerprint: kp.KeyFingerprint,
KeyName: kp.KeyName,
}
return response, nil
}
func (m *MockEC2) CreateKeyPairRequest(*ec2.CreateKeyPairInput) (*request.Request, *ec2.CreateKeyPairOutput) {
panic("MockEC2 CreateKeyPairRequest not implemented")
}
func (m *MockEC2) CreateKeyPairWithContext(aws.Context, *ec2.CreateKeyPairInput, ...request.Option) (*ec2.CreateKeyPairOutput, error) {
panic("Not implemented")
}
func (m *MockEC2) CreateKeyPair(*ec2.CreateKeyPairInput) (*ec2.CreateKeyPairOutput, error) {
panic("MockEC2 CreateKeyPair not implemented")
}
func (m *MockEC2) DescribeKeyPairs(request *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
glog.Infof("DescribeKeyPairs: %v", request)
var keypairs []*ec2.KeyPairInfo
for _, keypair := range m.KeyPairs {
allFiltersMatch := true
if len(request.KeyNames) != 0 {
match := false
for _, keyname := range request.KeyNames {
if aws.StringValue(keyname) == aws.StringValue(keypair.KeyName) {
match = true
}
}
if !match {
allFiltersMatch = false
}
}
for _, filter := range request.Filters {
match := false
switch *filter.Name {
case "key-name":
for _, v := range filter.Values {
if aws.StringValue(keypair.KeyName) == aws.StringValue(v) {
match = true
}
}
default:
return nil, fmt.Errorf("unknown filter name: %q", *filter.Name)
}
if !match {
allFiltersMatch = false
break
}
}
if !allFiltersMatch {
continue
}
copy := *keypair
keypairs = append(keypairs, ©)
}
response := &ec2.DescribeKeyPairsOutput{
KeyPairs: keypairs,
}
return response, nil
}
func (m *MockEC2) DeleteKeyPair(request *ec2.DeleteKeyPairInput) (*ec2.DeleteKeyPairOutput, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
glog.Infof("DeleteKeyPair: %v", request)
id := aws.StringValue(request.KeyName)
o := m.KeyPairs[id]
if o == nil {
return nil, fmt.Errorf("KeyPairs %q not found", id)
}
delete(m.KeyPairs, id)
return &ec2.DeleteKeyPairOutput{}, nil
}
func (m *MockEC2) DeleteKeyPairWithContext(aws.Context, *ec2.DeleteKeyPairInput, ...request.Option) (*ec2.DeleteKeyPairOutput, error) {
panic("Not implemented")
}
func (m *MockEC2) DeleteKeyPairRequest(*ec2.DeleteKeyPairInput) (*request.Request, *ec2.DeleteKeyPairOutput) {
panic("Not implemented")
}