Skip to content

Commit

Permalink
Merge pull request #71 from iits-consulting/Ninja243/error_output
Browse files Browse the repository at this point in the history
Fix oidc error exit number
  • Loading branch information
Ninja243 authored Nov 21, 2023
2 parents be0137c + 35feb88 commit ff94583
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 28 deletions.
10 changes: 5 additions & 5 deletions accesstoken/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func CreateAccessToken(tokenDescription string) {
log.Println("Creating access token file with GTC...")
log.Println("info: creating access token file with GTC...")
resp, err := getAccessTokenFromServiceProvider(tokenDescription)
if err != nil {
// A 404 error is thrown when trying to create a permanent AK/SK when logged in with OIDC or SAML
Expand Down Expand Up @@ -63,12 +63,12 @@ func makeAccessFile(resp *credentials.Credential, tempResp *credentials.Temporar
}

common.WriteStringToFile("./ak-sk-env.sh", accessKeyFileContent)
log.Println("Access token file created successfully")
log.Println("Please source the ak-sk-env.sh file in the current directory manually")
log.Println("info: access token file created successfully")
log.Println("info: please source the ak-sk-env.sh file in the current directory manually")
}

func CreateTemporaryAccessToken(durationSeconds int) error {
log.Println("Creating temporary access token file with GTC...")
log.Println("info: creating temporary access token file with GTC...")
resp, err := getTempAccessTokenFromServiceProvider(durationSeconds)
if err != nil {
return err
Expand Down Expand Up @@ -140,7 +140,7 @@ func handlePotentialLimitError(err error,

//nolint:gomnd // The OpenTelekomCloud only lets users have up to two keys
if len(accessTokens) == 2 {
log.Printf("Hit the limit for access keys on OTC. You can only have 2. Removing keys made by otc-auth...")
log.Printf("warning: hit the limit for access keys on OTC. You can only have 2. Removing keys made by otc-auth...")
return conditionallyReplaceAccessTokens(user, client, tokenDescription, accessTokens)
}
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions cce/cce.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetClusterNames(projectName string) config.Clusters {
}

config.UpdateClusters(clustersArr)
log.Printf("CCE Clusters for project %s:\n%s", projectName, strings.Join(clustersArr.GetClusterNames(), ",\n"))
log.Printf("info: CCE clusters for project %s:\n%s", projectName, strings.Join(clustersArr.GetClusterNames(), ",\n"))

return clustersArr
}
Expand Down Expand Up @@ -68,10 +68,10 @@ func GetKubeConfig(configParams KubeConfigParams, skipKubeTLS bool, printKubeCon
if err != nil {
log.Fatal(errWriter)
}
log.Printf("Successfully fetched kube config for cce cluster %s. \n", configParams.ClusterName)
log.Printf("info: successfully fetched kube config for cce cluster %s. \n", configParams.ClusterName)
} else {
mergeKubeConfig(configParams, kubeConfig)
log.Printf("Successfully fetched and merge kube config for cce cluster %s. \n", configParams.ClusterName)
log.Printf("info: successfully fetched and merge kube config for cce cluster %s. \n", configParams.ClusterName)
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func getClusterID(clusterName string, projectName string) (clusterID string, err
ID: cluster.Metadata.Id,
})
}
log.Printf("Clusters for project %s:\n%s", projectName, strings.Join(clusterArr.GetClusterNames(), ",\n"))
log.Printf("info: clusters for project %s:\n%s", projectName, strings.Join(clusterArr.GetClusterNames(), ",\n"))

config.UpdateClusters(clusterArr)
cloud = config.GetActiveCloudConfig()
Expand Down
2 changes: 1 addition & 1 deletion cce/kube_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func getKubeConfig(kubeConfigParams KubeConfigParams) (api.Config, error) {
log.Println("Getting kube config...")
log.Println("info: getting kube config...")

clusterID, err := getClusterID(kubeConfigParams.ClusterName, kubeConfigParams.ProjectName)
if err != nil {
Expand Down
25 changes: 16 additions & 9 deletions common/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,31 @@ func GetRequest(method string, url string, body io.Reader) *http.Request {
return request
}

func closeStreamCheckErr(body io.ReadCloser, err error) {
errBodyClose := body.Close()
if errBodyClose != nil {
err = fmt.Errorf("fatal: %w\nfatal: error closing response body\n%w", err, errBodyClose)
}
if err != nil {
log.Fatal(err)
}
}

func GetBodyBytesFromResponse(response *http.Response) []byte {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatalf("fatal: error closing response body.\ntrace: %s", err)
}
}(response.Body)
var err error

bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
log.Printf("fatal: error reading response body.\ntrace: %s", err)
err = fmt.Errorf("fatal: error reading response body\n%w", err)
closeStreamCheckErr(response.Body, err)
}

statusCodeStartsWith2 := regexp.MustCompile(`2\d{2}`)
if !statusCodeStartsWith2.MatchString(strconv.Itoa(response.StatusCode)) {
errorMessage := fmt.Sprintf("error: status %s, body:\n%s", response.Status, bodyBytes)
log.Print(errorMessage)
err = fmt.Errorf("fatal: status %s, body:\n%s", response.Status, bodyBytes)
closeStreamCheckErr(response.Body, err)
}

defer closeStreamCheckErr(response.Body, err)
return bodyBytes
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func LoadCloudConfig(domainName string) {
otcConfig.Clouds = clouds
writeOtcConfigContentToFile(otcConfig)

log.Printf("Cloud %s loaded successfully and set to active.\n", domainName)
log.Printf("info: cloud %s loaded successfully and set to active.\n", domainName)
}

func registerNewCloud(domainName string) Clouds {
Expand Down
6 changes: 3 additions & 3 deletions config/model.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"fmt"
"errors"
"log"
"time"

Expand Down Expand Up @@ -43,7 +43,7 @@ func (clouds *Clouds) SetActiveByName(name string) {

func (clouds *Clouds) FindActiveCloudConfigOrNil() (cloud *Cloud, index *int, err error) {
if clouds.NumberOfActiveCloudConfigs() > 1 {
return nil, nil, fmt.Errorf("more than one cloud active")
return nil, nil, errors.New("more than one cloud active")
}

for index, cloud := range *clouds {
Expand All @@ -52,7 +52,7 @@ func (clouds *Clouds) FindActiveCloudConfigOrNil() (cloud *Cloud, index *int, er
}
}

return nil, nil, fmt.Errorf("no active cloud")
return nil, nil, errors.New("no active cloud")
}

func (clouds *Clouds) GetActiveCloudIndex() int {
Expand Down
4 changes: 2 additions & 2 deletions iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func GetScopedToken(projectName string) config.Token {
}
}

log.Println("attempting to request a scoped token")
log.Printf("info: attempting to request a scoped token for %s\n", projectName)
cloud := getCloudWithScopedTokenFromServiceProvider(projectName)
config.UpdateCloudConfig(cloud)
log.Println("scoped token acquired successfully")
log.Println("info: scoped token acquired successfully")
project = config.GetActiveCloudConfig().Projects.GetProjectByNameOrThrow(projectName)
return project.ScopedToken
}
Expand Down
2 changes: 1 addition & 1 deletion iam/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetProjectsInActiveCloud() config.Projects {
}

config.UpdateProjects(cloudProjects)
log.Printf("Projects for active cloud:\n%s \n", strings.Join(cloudProjects.GetProjectNames(), ",\n"))
log.Printf("info: projects for active cloud:\n%s \n", strings.Join(cloudProjects.GetProjectNames(), ",\n"))
return cloudProjects
}

Expand Down
4 changes: 2 additions & 2 deletions login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func AuthenticateAndGetUnscopedToken(authInfo common.AuthInfo, skipTLS bool) {
return
}

log.Println("Retrieving unscoped token for active cloud...")
log.Println("info: retrieving unscoped token for active cloud...")

var tokenResponse common.TokenResponse
switch authInfo.AuthType {
Expand Down Expand Up @@ -53,7 +53,7 @@ func AuthenticateAndGetUnscopedToken(authInfo common.AuthInfo, skipTLS bool) {
}
updateOTCInfoFile(tokenResponse, authInfo.Region)
createScopedTokenForEveryProject()
log.Println("Successfully obtained unscoped token!")
log.Println("info: successfully obtained unscoped token!")
}

func createScopedTokenForEveryProject() {
Expand Down

0 comments on commit ff94583

Please sign in to comment.