keyring swap, better error messages/warnings, delay upgrade notif

This commit is contained in:
Maidul Islam
2023-08-01 21:11:30 -04:00
parent 285a6d633a
commit 7f69a3b23f
22 changed files with 265 additions and 604 deletions

View File

@@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"strings"
"github.com/Infisical/infisical-merge/packages/config"
"github.com/go-resty/resty/v2"
@@ -24,8 +25,10 @@ func CallGetEncryptedWorkspaceKey(httpClient *resty.Client, request GetEncrypted
return GetEncryptedWorkspaceKeyResponse{}, fmt.Errorf("CallGetEncryptedWorkspaceKey: Unable to complete api request [err=%s]", err)
}
PrintApiRequestDebugLog(response)
if response.IsError() {
return GetEncryptedWorkspaceKeyResponse{}, fmt.Errorf("CallGetEncryptedWorkspaceKey: Unsuccessful response: [response=%s]", response)
return GetEncryptedWorkspaceKeyResponse{}, PrintApiRequestError(response)
}
return result, nil
@@ -39,12 +42,14 @@ func CallGetServiceTokenDetailsV2(httpClient *resty.Client) (GetServiceTokenDeta
SetHeader("User-Agent", USER_AGENT).
Get(fmt.Sprintf("%v/v2/service-token", config.INFISICAL_URL))
PrintApiRequestDebugLog(response)
if err != nil {
return GetServiceTokenDetailsResponse{}, fmt.Errorf("CallGetServiceTokenDetails: Unable to complete api request [err=%s]", err)
}
if response.IsError() {
return GetServiceTokenDetailsResponse{}, fmt.Errorf("CallGetServiceTokenDetails: Unsuccessful response: [response=%s]", response)
return GetServiceTokenDetailsResponse{}, PrintApiRequestError(response)
}
return tokenDetailsResponse, nil
@@ -59,12 +64,14 @@ func CallLogin1V2(httpClient *resty.Client, request GetLoginOneV2Request) (GetLo
SetBody(request).
Post(fmt.Sprintf("%v/v2/auth/login1", config.INFISICAL_URL))
PrintApiRequestDebugLog(response)
if err != nil {
return GetLoginOneV2Response{}, fmt.Errorf("CallLogin1V2: Unable to complete api request [err=%s]", err)
}
if response.IsError() {
return GetLoginOneV2Response{}, fmt.Errorf("CallLogin1V2: Unsuccessful response: [response=%s]", response)
return GetLoginOneV2Response{}, PrintApiRequestError(response)
}
return loginOneV2Response, nil
@@ -97,6 +104,8 @@ func CallVerifyMfaToken(httpClient *resty.Client, request VerifyMfaTokenRequest)
verifyMfaTokenResponse.RefreshToken = refreshToken.Value
}
PrintApiRequestDebugLog(response)
if err != nil {
return nil, nil, fmt.Errorf("CallVerifyMfaToken: Unable to complete api request [err=%s]", err)
}
@@ -133,12 +142,14 @@ func CallLogin2V2(httpClient *resty.Client, request GetLoginTwoV2Request) (GetLo
loginTwoV2Response.RefreshToken = refreshToken.Value
}
PrintApiRequestDebugLog(response)
if err != nil {
return GetLoginTwoV2Response{}, fmt.Errorf("CallLogin2V2: Unable to complete api request [err=%s]", err)
}
if response.IsError() {
return GetLoginTwoV2Response{}, fmt.Errorf("CallLogin2V2: Unsuccessful response: [response=%s]", response)
return GetLoginTwoV2Response{}, PrintApiRequestError(response)
}
return loginTwoV2Response, nil
@@ -152,12 +163,14 @@ func CallGetAllWorkSpacesUserBelongsTo(httpClient *resty.Client) (GetWorkSpacesR
SetHeader("User-Agent", USER_AGENT).
Get(fmt.Sprintf("%v/v1/workspace", config.INFISICAL_URL))
PrintApiRequestDebugLog(response)
if err != nil {
return GetWorkSpacesResponse{}, err
}
if response.IsError() {
return GetWorkSpacesResponse{}, fmt.Errorf("CallGetAllWorkSpacesUserBelongsTo: Unsuccessful response: [response=%v]", response)
return GetWorkSpacesResponse{}, PrintApiRequestError(response)
}
return workSpacesResponse, nil
@@ -171,6 +184,8 @@ func CallIsAuthenticated(httpClient *resty.Client) bool {
SetHeader("User-Agent", USER_AGENT).
Post(fmt.Sprintf("%v/v1/auth/checkAuth", config.INFISICAL_URL))
PrintApiRequestDebugLog(response)
if err != nil {
return false
}
@@ -191,12 +206,14 @@ func CallGetAccessibleEnvironments(httpClient *resty.Client, request GetAccessib
SetHeader("User-Agent", USER_AGENT).
Get(fmt.Sprintf("%v/v2/workspace/%s/environments", config.INFISICAL_URL, request.WorkspaceId))
PrintApiRequestDebugLog(response)
if err != nil {
return GetAccessibleEnvironmentsResponse{}, err
}
if response.IsError() {
return GetAccessibleEnvironmentsResponse{}, fmt.Errorf("CallGetAccessibleEnvironments: Unsuccessful response: [response=%v]", response)
return GetAccessibleEnvironmentsResponse{}, PrintApiRequestError(response)
}
return accessibleEnvironmentsResponse, nil
@@ -214,12 +231,14 @@ func CallGetNewAccessTokenWithRefreshToken(httpClient *resty.Client, refreshToke
}).
Post(fmt.Sprintf("%v/v1/auth/token", config.INFISICAL_URL))
PrintApiRequestDebugLog(response)
if err != nil {
return GetNewAccessTokenWithRefreshTokenResponse{}, err
}
if response.IsError() {
return GetNewAccessTokenWithRefreshTokenResponse{}, fmt.Errorf("CallGetNewAccessTokenWithRefreshToken: Unsuccessful response: [response=%v]", response)
return GetNewAccessTokenWithRefreshTokenResponse{}, PrintApiRequestError(response)
}
return newAccessToken, nil
@@ -245,6 +264,8 @@ func CallGetSecretsV3(httpClient *resty.Client, request GetEncryptedSecretsV3Req
response, err := httpRequest.Get(fmt.Sprintf("%v/v3/secrets", config.INFISICAL_URL))
PrintApiRequestDebugLog(response)
if err != nil {
return GetEncryptedSecretsV3Response{}, fmt.Errorf("CallGetSecretsV3: Unable to complete api request [err=%s]", err)
}
@@ -253,7 +274,7 @@ func CallGetSecretsV3(httpClient *resty.Client, request GetEncryptedSecretsV3Req
if response.StatusCode() == 401 {
return GetEncryptedSecretsV3Response{}, fmt.Errorf("CallGetSecretsV3: Request to access secrets with [environment=%v] [path=%v] [workspaceId=%v] is denied. Please check if your authentication method has access to requested scope", request.Environment, request.SecretPath, request.WorkspaceId)
} else {
return GetEncryptedSecretsV3Response{}, fmt.Errorf("CallGetSecretsV3: Unsuccessful response. Please make sure your secret path, workspace and environment name are all correct [response=%v]", response.RawResponse)
return GetEncryptedSecretsV3Response{}, fmt.Errorf("CallGetSecretsV3: Unsuccessful response with http [status=%v]. Please make sure your secret path, workspace and environment name are all correct", response.StatusCode())
}
}
@@ -269,12 +290,14 @@ func CallCreateSecretsV3(httpClient *resty.Client, request CreateSecretV3Request
SetBody(request).
Post(fmt.Sprintf("%v/v3/secrets/%s", config.INFISICAL_URL, request.SecretName))
PrintApiRequestDebugLog(response)
if err != nil {
return fmt.Errorf("CallCreateSecretsV3: Unable to complete api request [err=%s]", err)
}
if response.IsError() {
return fmt.Errorf("CallCreateSecretsV3: Unsuccessful response. Please make sure your secret path, workspace and environment name are all correct [response=%s]", response)
return fmt.Errorf("CallCreateSecretsV3: Unsuccessful response with http [status=%v]. Please make sure your secret path, workspace and environment name are all correct", response.StatusCode())
}
return nil
@@ -289,12 +312,14 @@ func CallDeleteSecretsV3(httpClient *resty.Client, request DeleteSecretV3Request
SetBody(request).
Delete(fmt.Sprintf("%v/v3/secrets/%s", config.INFISICAL_URL, request.SecretName))
PrintApiRequestDebugLog(response)
if err != nil {
return fmt.Errorf("CallDeleteSecretsV3: Unable to complete api request [err=%s]", err)
}
if response.IsError() {
return fmt.Errorf("CallDeleteSecretsV3: Unsuccessful response. Please make sure your secret path, workspace and environment name are all correct [response=%s]", response)
return fmt.Errorf("CallDeleteSecretsV3: Unsuccessful response with http [status=%v]. Please make sure your secret path, workspace and environment name are all correct", response.StatusCode())
}
return nil
@@ -309,6 +334,8 @@ func CallUpdateSecretsV3(httpClient *resty.Client, request UpdateSecretByNameV3R
SetBody(request).
Patch(fmt.Sprintf("%v/v3/secrets/%s", config.INFISICAL_URL, request.SecretName))
PrintApiRequestDebugLog(response)
if err != nil {
return fmt.Errorf("CallUpdateSecretsV3: Unable to complete api request [err=%s]", err)
}
@@ -329,6 +356,8 @@ func CallGetSingleSecretByNameV3(httpClient *resty.Client, request CreateSecretV
SetBody(request).
Post(fmt.Sprintf("%v/v3/secrets/%s", config.INFISICAL_URL, request.SecretName))
PrintApiRequestDebugLog(response)
if err != nil {
return fmt.Errorf("CallGetSingleSecretByNameV3: Unable to complete api request [err=%s]", err)
}
@@ -339,3 +368,17 @@ func CallGetSingleSecretByNameV3(httpClient *resty.Client, request CreateSecretV
return nil
}
// API helper
func PrintApiRequestError(response *resty.Response) error {
method := strings.ToUpper(response.Request.Method)
url := response.Request.URL
responseStatus := response.StatusCode()
return fmt.Errorf("request to call %v %v resulted in response status %v", method, url, responseStatus)
}
func PrintApiRequestDebugLog(response *resty.Response) {
method := strings.ToUpper(response.Request.Method)
url := response.Request.URL
log.Debug().Msgf("requesting to call %v %v with [request=%v] returned [response=%v]", method, url, response.Request, response.RawResponse)
}