This repository was archived by the owner on May 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (52 loc) · 1.75 KB
/
main.go
File metadata and controls
60 lines (52 loc) · 1.75 KB
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/joho/godotenv"
"github.com/luizm/eks-login/internal/eks"
"github.com/luizm/eks-login/internal/vault"
)
const version string = "v1.0.1"
var homeDir = os.Getenv("HOME")
var eksLoginDir = filepath.Join(os.Getenv("HOME"), ".eks-login")
func main() {
clusterName := flag.String("cluster-name", "k8s-sandbox", "EKS cluster name, you can see this name in EKS console")
region := flag.String("region", "us-east-1", "AWS region where EKS cluster is running")
vaultAddr := flag.String("vault-addr", "", "The vault address, example: https://your.vault.domain")
vaultPath := flag.String("vault-path", "aws/creds/"+*clusterName, "The vault endpoint path, example: aws/creds/clustername")
githubTokenPath := flag.String("github-token-path", homeDir+"/.github-token", "Path to file with github credential")
appVersion := flag.Bool("version", false, "Shows application version")
flag.Parse()
if *appVersion == true {
out := fmt.Sprintf("%s %s", "eks-login", version)
fmt.Println(out)
os.Exit(0)
}
if *vaultAddr == "" {
flag.PrintDefaults()
os.Exit(1)
}
godotenv.Load(filepath.Join(eksLoginDir, *clusterName))
if !vault.LeaseIsValid() {
if content, err := vault.FetchAwsCredsFromVault(*clusterName, *vaultAddr, *vaultPath, *githubTokenPath); err != nil {
log.Fatalln(err)
} else {
if err := os.MkdirAll(eksLoginDir, 0700); err != nil {
log.Fatalln(err)
}
if err := ioutil.WriteFile(filepath.Join(eksLoginDir, *clusterName), []byte(content), 0644); err != nil {
log.Fatalln(err)
}
godotenv.Load(filepath.Join(eksLoginDir, *clusterName))
}
}
out, err := eks.GetEKSToken(*clusterName, *region)
if err != nil {
log.Fatalln(err)
}
fmt.Println(out)
}