/
options.go
209 lines (180 loc) · 5.75 KB
/
options.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package kubectl
import (
"encoding/base64"
"io/ioutil"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/gruntwork-io/go-commons/errors"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"github.com/gruntwork-io/kubergrunt/eksawshelper"
"github.com/gruntwork-io/kubergrunt/logging"
)
// AuthScheme is an enum that indicates how to authenticate to the Kubernetes cluster.
type AuthScheme int
const (
ConfigBased AuthScheme = iota
DirectAuth
EKSClusterBased
)
// Represents common options necessary to specify for all Kubectl calls
type KubectlOptions struct {
// Config based authentication scheme
ContextName string
ConfigPath string
// Direct authentication scheme. Has precedence over config based scheme. All 3 values must be set.
Server string
Base64PEMCertificateAuthority string
BearerToken string
// EKS based authentication scheme. Has precedence over direct or config based scheme.
EKSClusterArn string
}
type serverInfo struct {
Server string
Base64PEMCertificateAuthority string
BearerToken string
}
// TempConfigFromAuthInfo will create a temporary kubeconfig file that can be used with commands that don't support
// directly configuring auth info (e.g helm).
func (options *KubectlOptions) TempConfigFromAuthInfo() (string, error) {
logger := logging.GetProjectLogger()
logger.Infof("Creating temporary file to act as kubeconfig with auth info")
tmpfile, err := ioutil.TempFile("", "")
if err != nil {
return "", errors.WithStackTrace(err)
}
err = tmpfile.Close()
if err != nil {
return tmpfile.Name(), errors.WithStackTrace(err)
}
logger.Infof("Created %s to act as temporary kubeconfig file.", tmpfile.Name())
scheme := options.AuthScheme()
switch scheme {
case DirectAuth:
err = tempConfigFromDirectAuthInfo(
logger,
tmpfile,
serverInfo{
Server: options.Server,
Base64PEMCertificateAuthority: options.Base64PEMCertificateAuthority,
BearerToken: options.BearerToken,
},
)
case EKSClusterBased:
err = tempConfigFromEKSClusterInfo(logger, tmpfile, options.EKSClusterArn)
default:
return "", errors.WithStackTrace(AuthSchemeNotSupported{scheme})
}
return tmpfile.Name(), err
}
func tempConfigFromDirectAuthInfo(logger *logrus.Entry, tmpfile *os.File, serverInfo serverInfo) error {
config := api.NewConfig()
err := AddClusterToConfig(
config,
"default",
serverInfo.Server,
serverInfo.Base64PEMCertificateAuthority,
)
if err != nil {
return err
}
logger.Infof("Adding auth info to config")
authInfo := api.NewAuthInfo()
authInfo.Token = serverInfo.BearerToken
config.AuthInfos["default"] = authInfo
logger.Infof("Done adding auth info to config")
err = AddContextToConfig(
config,
"default",
"default",
"default",
)
if err != nil {
return err
}
config.CurrentContext = "default"
if err := saveConfigToFile(config, tmpfile); err != nil {
return errors.WithStackTrace(err)
}
return nil
}
func tempConfigFromEKSClusterInfo(logger *logrus.Entry, tmpfile *os.File, eksClusterArn string) error {
info, err := getKubeCredentialsFromEKSCluster(eksClusterArn)
if err != nil {
return err
}
return tempConfigFromDirectAuthInfo(logger, tmpfile, *info)
}
func saveConfigToFile(config *api.Config, tmpfile *os.File) error {
// In order to be able to render and save the config, we need to have the config file be in a format that k8s
// understands, so we first initialize the empty file with a minimal config structure.
if err := CreateInitialConfig(tmpfile.Name()); err != nil {
return err
}
// Then load the contents into a struct that can be saved, and save the generated config data.
kubeconfig := LoadConfigFromPath(tmpfile.Name())
if err := clientcmd.ModifyConfig(kubeconfig.ConfigAccess(), *config, false); err != nil {
return errors.WithStackTrace(err)
}
return nil
}
func getKubeCredentialsFromEKSCluster(eksClusterArn string) (*serverInfo, error) {
cluster, err := eksawshelper.GetClusterByArn(eksClusterArn)
if err != nil {
return nil, err
}
server := aws.StringValue(cluster.Endpoint)
b64PEMCA := aws.StringValue(cluster.CertificateAuthority.Data)
clusterName, err := eksawshelper.GetClusterNameFromArn(eksClusterArn)
if err != nil {
return nil, err
}
token, _, err := eksawshelper.GetKubernetesTokenForCluster(clusterName)
if err != nil {
return nil, err
}
info := serverInfo{
Server: server,
Base64PEMCertificateAuthority: b64PEMCA,
BearerToken: token.Token,
}
return &info, nil
}
// TempCAFile creates a temporary file to hold the Certificate Authority data so that it can be passed on to kubectl.
func (options *KubectlOptions) TempCAFile() (string, error) {
logger := logging.GetProjectLogger()
logger.Infof("Creating temporary file to hold certificate authority data")
tmpfile, err := ioutil.TempFile("", "")
if err != nil {
return "", errors.WithStackTrace(err)
}
defer tmpfile.Close()
logger.Infof("Created %s to hold certificate authority data.", tmpfile.Name())
caData, err := base64.StdEncoding.DecodeString(options.Base64PEMCertificateAuthority)
if err != nil {
return tmpfile.Name(), errors.WithStackTrace(err)
}
_, err = tmpfile.Write(caData)
return tmpfile.Name(), errors.WithStackTrace(err)
}
func (options *KubectlOptions) AuthScheme() AuthScheme {
if options.EKSClusterArn != "" {
return EKSClusterBased
} else if options.Server != "" {
return DirectAuth
}
return ConfigBased
}
func authSchemeToString(scheme AuthScheme) string {
switch scheme {
case ConfigBased:
return "config-based"
case DirectAuth:
return "direct"
case EKSClusterBased:
return "eks-cluster-based"
}
// This should not happen
return "unspecified"
}