github.com/microsoft/moc@v0.17.1/pkg/auth/auth_env.go (about) 1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the Apache v2.0 license. 3 4 package auth 5 6 import ( 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 const ( 13 ClientTokenName = ".token" 14 ClientCertName = "wssd.pem" 15 ClientTokenPath = "WSSD_CLIENT_TOKEN" 16 WssdConfigPath = "WSSD_CONFIG_PATH" 17 AccessFileDirPath = "ACCESSFILE_DIR_PATH" 18 DefaultWSSDFolder = ".wssd" 19 AccessFileDefaultName = "cloudconfig" 20 ) 21 22 func getExecutableName() (string, error) { 23 execPath, err := os.Executable() 24 if err != nil { 25 return "", err 26 } 27 return strings.TrimSuffix(filepath.Base(execPath), filepath.Ext(execPath)), nil 28 } 29 30 // SetCertificateDirPath sets the directory path where the client certificate will be stored 31 // This is achieved by setting ACCESSFILE_DIR_PATH environment variable 32 // The path is appended with the executable name before the certificate is stored 33 func SetCertificateDirPath(certificateDirPath string) error { 34 return os.Setenv(AccessFileDirPath, certificateDirPath) 35 } 36 37 // SetCertificateFilePath sets the file path where the client certificate will be stored 38 // This is achieved by setting WSSD_CONFIG_PATH environment variable 39 func SetCertificateFilePath(certificateFilePath string) error { 40 return os.Setenv(WssdConfigPath, certificateFilePath) 41 } 42 43 // SetLoginTokenPath sets the path where the login yaml will be stored 44 // This is achieved by setting WSSD_CLIENT_TOKEN environment variable 45 // The path is appended with the executable name before the certificate is stored 46 func SetLoginTokenPath(loginConfigPath string) error { 47 return os.Setenv(ClientTokenPath, loginConfigPath) 48 } 49 50 // GetCertificateDirPath will return the directory path where the client certificate will be stored 51 func GetCertificateDirPath() string { 52 return os.Getenv(AccessFileDirPath) 53 } 54 55 // GetCertificateFilePath will return the file path where the client certificate will be stored 56 func GetCertificateFilePath() string { 57 return os.Getenv(WssdConfigPath) 58 } 59 60 // GetLoginTokenPath will return the file path where the login yaml will be stored 61 func GetLoginTokenPath() string { 62 return os.Getenv(ClientTokenPath) 63 } 64 65 // GetWssdConfigLocation gets the path for access file from environment 66 func GetWssdConfigLocation() string { 67 accessFileDirPath := os.Getenv(AccessFileDirPath) 68 wssdConfigPath := os.Getenv(WssdConfigPath) 69 defaultPath := accessFileDirPath 70 71 if accessFileDirPath == "" && wssdConfigPath != "" { 72 return wssdConfigPath 73 } 74 75 if accessFileDirPath == "" && wssdConfigPath == "" { 76 wd, err := os.UserHomeDir() 77 if err != nil { 78 panic(err) 79 } 80 81 // Create the default config path and set the 82 // env variable 83 defaultPath = filepath.Join(wd, DefaultWSSDFolder) 84 os.Setenv(AccessFileDirPath, defaultPath) 85 } 86 87 if execName, err := getExecutableName(); err == nil { 88 defaultPath = filepath.Join(defaultPath, execName) 89 } 90 os.MkdirAll(defaultPath, os.ModePerm) 91 accessFilePath := filepath.Join(defaultPath, AccessFileDefaultName) 92 return accessFilePath 93 } 94 95 // GetWssdConfigLocationName gets the path for access filename from environment + subfolder with file name fileName 96 func GetMocConfigLocationName(subfolder, filename string) string { 97 wssdConfigPath := os.Getenv(WssdConfigPath) 98 99 file := AccessFileDefaultName 100 if filename != "" { 101 file = filename 102 } 103 wd, err := os.UserHomeDir() 104 if err != nil { 105 panic(err) 106 } 107 if wssdConfigPath == "" || !strings.HasSuffix(wssdConfigPath, filepath.Join(wd, subfolder, file)) { 108 // Create the default config path and set the 109 // env variable 110 defaultPath := filepath.Join(wd, DefaultWSSDFolder, subfolder) 111 os.MkdirAll(defaultPath, os.ModePerm) 112 wssdConfigPath = filepath.Join(defaultPath, file) 113 os.Setenv(WssdConfigPath, wssdConfigPath) 114 } 115 return wssdConfigPath 116 } 117 118 func getClientTokenLocation() string { 119 clientTokenPath := os.Getenv(ClientTokenPath) 120 if clientTokenPath == "" { 121 wd, err := os.UserHomeDir() 122 if err != nil { 123 panic(err) 124 } 125 126 // Create the default token path and set the 127 // env variable 128 defaultPath := filepath.Join(wd, DefaultWSSDFolder) 129 os.MkdirAll(defaultPath, os.ModePerm) 130 clientTokenPath = filepath.Join(defaultPath, ClientTokenName) 131 os.Setenv(ClientTokenPath, clientTokenPath) 132 } 133 return clientTokenPath 134 }