-
Notifications
You must be signed in to change notification settings - Fork 48
/
auth_env.go
134 lines (115 loc) · 4.12 KB
/
auth_env.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
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache v2.0 license.
package auth
import (
"os"
"path/filepath"
"strings"
)
const (
ClientTokenName = ".token"
ClientCertName = "wssd.pem"
ClientTokenPath = "WSSD_CLIENT_TOKEN"
WssdConfigPath = "WSSD_CONFIG_PATH"
AccessFileDirPath = "ACCESSFILE_DIR_PATH"
DefaultWSSDFolder = ".wssd"
AccessFileDefaultName = "cloudconfig"
)
func getExecutableName() (string, error) {
execPath, err := os.Executable()
if err != nil {
return "", err
}
return strings.TrimSuffix(filepath.Base(execPath), filepath.Ext(execPath)), nil
}
// SetCertificateDirPath sets the directory path where the client certificate will be stored
// This is achieved by setting ACCESSFILE_DIR_PATH environment variable
// The path is appended with the executable name before the certificate is stored
func SetCertificateDirPath(certificateDirPath string) error {
return os.Setenv(AccessFileDirPath, certificateDirPath)
}
// SetCertificateFilePath sets the file path where the client certificate will be stored
// This is achieved by setting WSSD_CONFIG_PATH environment variable
func SetCertificateFilePath(certificateFilePath string) error {
return os.Setenv(WssdConfigPath, certificateFilePath)
}
// SetLoginTokenPath sets the path where the login yaml will be stored
// This is achieved by setting WSSD_CLIENT_TOKEN environment variable
// The path is appended with the executable name before the certificate is stored
func SetLoginTokenPath(loginConfigPath string) error {
return os.Setenv(ClientTokenPath, loginConfigPath)
}
// GetCertificateDirPath will return the directory path where the client certificate will be stored
func GetCertificateDirPath() string {
return os.Getenv(AccessFileDirPath)
}
// GetCertificateFilePath will return the file path where the client certificate will be stored
func GetCertificateFilePath() string {
return os.Getenv(WssdConfigPath)
}
// GetLoginTokenPath will return the file path where the login yaml will be stored
func GetLoginTokenPath() string {
return os.Getenv(ClientTokenPath)
}
// GetWssdConfigLocation gets the path for access file from environment
func GetWssdConfigLocation() string {
accessFileDirPath := os.Getenv(AccessFileDirPath)
wssdConfigPath := os.Getenv(WssdConfigPath)
defaultPath := accessFileDirPath
if accessFileDirPath == "" && wssdConfigPath != "" {
return wssdConfigPath
}
if accessFileDirPath == "" && wssdConfigPath == "" {
wd, err := os.UserHomeDir()
if err != nil {
panic(err)
}
// Create the default config path and set the
// env variable
defaultPath = filepath.Join(wd, DefaultWSSDFolder)
os.Setenv(AccessFileDirPath, defaultPath)
}
if execName, err := getExecutableName(); err == nil {
defaultPath = filepath.Join(defaultPath, execName)
}
os.MkdirAll(defaultPath, os.ModePerm)
accessFilePath := filepath.Join(defaultPath, AccessFileDefaultName)
return accessFilePath
}
// GetWssdConfigLocationName gets the path for access filename from environment + subfolder with file name fileName
func GetMocConfigLocationName(subfolder, filename string) string {
wssdConfigPath := os.Getenv(WssdConfigPath)
file := AccessFileDefaultName
if filename != "" {
file = filename
}
wd, err := os.UserHomeDir()
if err != nil {
panic(err)
}
if wssdConfigPath == "" || !strings.HasSuffix(wssdConfigPath, filepath.Join(wd, subfolder, file)) {
// Create the default config path and set the
// env variable
defaultPath := filepath.Join(wd, DefaultWSSDFolder, subfolder)
os.MkdirAll(defaultPath, os.ModePerm)
wssdConfigPath = filepath.Join(defaultPath, file)
os.Setenv(WssdConfigPath, wssdConfigPath)
}
return wssdConfigPath
}
func getClientTokenLocation() string {
clientTokenPath := os.Getenv(ClientTokenPath)
if clientTokenPath == "" {
wd, err := os.UserHomeDir()
if err != nil {
panic(err)
}
// Create the default token path and set the
// env variable
defaultPath := filepath.Join(wd, DefaultWSSDFolder)
os.MkdirAll(defaultPath, os.ModePerm)
clientTokenPath = filepath.Join(defaultPath, ClientTokenName)
os.Setenv(ClientTokenPath, clientTokenPath)
}
return clientTokenPath
}