github.com/jenkins-x/jx/v2@v2.1.155/pkg/cloud/iks/auth.go (about) 1 package iks 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 gohttp "net/http" 7 "os" 8 "os/user" 9 10 ibmcloud "github.com/IBM-Cloud/bluemix-go" 11 "github.com/IBM-Cloud/bluemix-go/http" 12 "github.com/IBM-Cloud/bluemix-go/rest" 13 ) 14 15 type ConfigJSON struct { 16 APIEndpoint string `json:"APIEndpoint"` 17 ConsoleEndpoint string `json:"ConsoleEndpoint"` 18 Region string `json:"Region"` 19 RegionID string `json:"RegionID"` 20 RegionType string `json:"RegionType"` 21 IAMEndpoint string `json:"IAMEndpoint"` 22 IAMToken string `json:"IAMToken"` 23 IAMRefreshToken string `json:"IAMRefreshToken"` 24 Account struct { 25 GUID string `json:"GUID"` 26 Name string `json:"Name"` 27 Owner string `json:"Owner"` 28 } `json:"Account"` 29 ResourceGroup struct { 30 GUID string `json:"GUID"` 31 Name string `json:"Name"` 32 State string `json:"State"` 33 Default bool `json:"Default"` 34 QuotaID string `json:"QuotaID"` 35 } `json:"ResourceGroup"` 36 CFEETargeted bool `json:"CFEETargeted"` 37 CFEEEnvID string `json:"CFEEEnvID"` 38 PluginRepos []struct { 39 Name string `json:"Name"` 40 URL string `json:"URL"` 41 } `json:"PluginRepos"` 42 SSLDisabled bool `json:"SSLDisabled"` 43 Locale string `json:"Locale"` 44 Trace string `json:"Trace"` 45 ColorEnabled string `json:"ColorEnabled"` 46 HTTPTimeout int `json:"HTTPTimeout"` 47 CLIInfoEndpoint string `json:"CLIInfoEndpoint"` 48 CheckCLIVersionDisabled bool `json:"CheckCLIVersionDisabled"` 49 UsageStatsDisabled bool `json:"UsageStatsDisabled"` 50 SDKVersion string `json:"SDKVersion"` 51 UpdateCheckInterval int `json:"UpdateCheckInterval"` 52 UpdateRetryCheckInterval int `json:"UpdateRetryCheckInterval"` 53 UpdateNotificationInterval int `json:"UpdateNotificationInterval"` 54 } 55 56 func ConfigFromJSON(config *ibmcloud.Config) (accountID string, err error) { 57 configjson := new(ConfigJSON) 58 usr, err := user.Current() 59 if err != nil { 60 return "", err 61 } 62 63 jsonFile, err := os.Open(usr.HomeDir + "/.bluemix/config.json") 64 if err != nil { 65 return "", err 66 } 67 68 defer jsonFile.Close() //nolint:errcheck 69 byteValue, _ := ioutil.ReadAll(jsonFile) 70 71 err = json.Unmarshal(byteValue, configjson) 72 if err != nil { 73 return "", err 74 } 75 76 config.Region = configjson.Region 77 config.IAMAccessToken = configjson.IAMToken 78 config.IAMRefreshToken = configjson.IAMRefreshToken 79 config.SSLDisable = configjson.SSLDisabled 80 config.Region = configjson.Region 81 config.BluemixAPIKey = "fake" 82 config.IBMID = "fake" 83 config.IBMIDPassword = "fake" 84 accountID = configjson.Account.GUID 85 86 return accountID, nil 87 } 88 89 func getIAMAuthRepository(config *ibmcloud.Config) (*IAMAuthRepository, error) { 90 if config.HTTPClient == nil { 91 config.HTTPClient = http.NewHTTPClient(config) 92 } 93 return NewIAMAuthRepository(config, &rest.Client{ 94 DefaultHeader: gohttp.Header{ 95 "User-Agent": []string{http.UserAgent()}, 96 }, 97 HTTPClient: config.HTTPClient, 98 }) 99 } 100 101 func AuthenticateSSO(passcode string, config *ibmcloud.Config) error { 102 config.IBMIDPassword = passcode 103 config.IBMID = passcode 104 config.Endpoint = nil 105 iamauthrepo, err := getIAMAuthRepository(config) 106 if err != nil { 107 return nil 108 } 109 return iamauthrepo.AuthenticateSSO(passcode) 110 } 111 112 func AuthenticatePassword(username string, password string, config *ibmcloud.Config) error { 113 config.IBMIDPassword = password 114 config.IBMID = username 115 config.Endpoint = nil 116 iamauthrepo, err := getIAMAuthRepository(config) 117 if err != nil { 118 return nil 119 } 120 return iamauthrepo.AuthenticatePassword(username, password) 121 } 122 123 func AuthenticateAPIKey(apikey string, config *ibmcloud.Config) error { 124 config.BluemixAPIKey = apikey 125 config.Endpoint = nil 126 iamauthrepo, err := getIAMAuthRepository(config) 127 if err != nil { 128 return nil 129 } 130 return iamauthrepo.AuthenticateAPIKey(apikey) 131 } 132 133 func RefreshTokenToLinkAccounts(account *Account, config *ibmcloud.Config) error { 134 config.Endpoint = nil 135 iamauthrepo, err := getIAMAuthRepository(config) 136 if err != nil { 137 return nil 138 } 139 return iamauthrepo.RefreshTokenToLinkAccounts(account) 140 }