/
root.go
156 lines (129 loc) · 5.13 KB
/
root.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 2017 by the contributors.
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 main
import (
"errors"
"fmt"
"os"
"sigs.k8s.io/aws-iam-authenticator/pkg/config"
"sigs.k8s.io/aws-iam-authenticator/pkg/mapper"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/component-base/featuregate"
)
var cfgFile string
var rootCmd = &cobra.Command{
Use: "aws-iam-authenticator",
Short: "A tool to authenticate to Kubernetes using AWS IAM credentials",
}
var featureGates = featuregate.NewFeatureGate()
func main() {
Execute()
}
// Execute the CLI entrypoint
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Load configuration from `filename`")
rootCmd.PersistentFlags().StringP("log-format", "l", "text", "Specify log format to use when logging to stderr [text or json]")
rootCmd.PersistentFlags().StringP(
"cluster-id",
"i",
"",
"Specify the cluster `ID`, a unique-per-cluster identifier for your aws-iam-authenticator installation.")
viper.BindPFlag("clusterID", rootCmd.PersistentFlags().Lookup("cluster-id"))
viper.BindEnv("clusterID", "KUBERNETES_AWS_AUTHENTICATOR_CLUSTER_ID")
featureGates.Add(config.DefaultFeatureGates)
featureGates.AddFlag(rootCmd.PersistentFlags())
viper.BindPFlag("feature-gates", rootCmd.PersistentFlags().Lookup("feature-gates"))
}
func initConfig() {
logrus.SetFormatter(getLogFormatter())
if cfgFile == "" {
return
}
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Can't read configuration file %q: %v\n", cfgFile, err)
os.Exit(1)
}
}
func getConfig() (config.Config, error) {
cfg := config.Config{
PartitionID: viper.GetString("server.partition"),
ClusterID: viper.GetString("clusterID"),
ServerEC2DescribeInstancesRoleARN: viper.GetString("server.ec2DescribeInstancesRoleARN"),
HostPort: viper.GetInt("server.port"),
Hostname: viper.GetString("server.hostname"),
GenerateKubeconfigPath: viper.GetString("server.generateKubeconfig"),
KubeconfigPregenerated: viper.GetBool("server.kubeconfigPregenerated"),
StateDir: viper.GetString("server.stateDir"),
Address: viper.GetString("server.address"),
Kubeconfig: viper.GetString("server.kubeconfig"),
Master: viper.GetString("server.master"),
BackendMode: viper.GetStringSlice("server.backendMode"),
EC2DescribeInstancesQps: viper.GetInt("server.ec2DescribeInstancesQps"),
EC2DescribeInstancesBurst: viper.GetInt("server.ec2DescribeInstancesBurst"),
ScrubbedAWSAccounts: viper.GetStringSlice("server.scrubbedAccounts"),
EKSYaml: viper.GetBool("server.eksyaml"),
}
if err := viper.UnmarshalKey("server.mapRoles", &cfg.RoleMappings); err != nil {
return cfg, fmt.Errorf("invalid server role mappings: %v", err)
}
if err := viper.UnmarshalKey("server.mapUsers", &cfg.UserMappings); err != nil {
logrus.WithError(err).Fatal("invalid server user mappings")
}
if err := viper.UnmarshalKey("server.mapAccounts", &cfg.AutoMappedAWSAccounts); err != nil {
logrus.WithError(err).Fatal("invalid server account mappings")
}
if featureGates.Enabled(config.SSORoleMatch) {
logrus.Info("SSORoleMatch feature enabled")
config.SSORoleMatchEnabled = true
}
if featureGates.Enabled(config.ConfiguredInitDirectories) {
logrus.Info("ConfiguredInitDirectories feature enabled")
}
if cfg.ClusterID == "" {
return cfg, errors.New("cluster ID cannot be empty")
}
partitionKeys := []string{}
partitionMap := map[string]endpoints.Partition{}
for _, p := range endpoints.DefaultPartitions() {
partitionMap[p.ID()] = p
partitionKeys = append(partitionKeys, p.ID())
}
if _, ok := partitionMap[cfg.PartitionID]; !ok {
return cfg, errors.New("Invalid partition")
}
if errs := mapper.ValidateBackendMode(cfg.BackendMode); len(errs) > 0 {
return cfg, utilerrors.NewAggregate(errs)
}
return cfg, nil
}
func getLogFormatter() logrus.Formatter {
format, _ := rootCmd.PersistentFlags().GetString("log-format")
if format == "json" {
return &logrus.JSONFormatter{}
} else if format != "text" {
logrus.Warnf("Unknown log format specified (%s), will use default text formatter instead.", format)
}
return &logrus.TextFormatter{FullTimestamp: true}
}