Skip to content

Commit

Permalink
switch k8-operator to secrets v2api
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Jan 16, 2023
1 parent da3742f commit 3ba636f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 42 deletions.
2 changes: 1 addition & 1 deletion k8-operator/config/samples/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-2
name: nginx-deployment
labels:
app: nginx
annotations:
Expand Down
24 changes: 12 additions & 12 deletions k8-operator/packages/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func CallGetEncryptedWorkspaceKey(httpClient *resty.Client, request GetEncrypted
return GetEncryptedWorkspaceKeyResponse{}, fmt.Errorf("CallGetEncryptedWorkspaceKey: Unable to complete api request [err=%s]", err)
}

if response.StatusCode() > 299 {
if response.IsError() {
return GetEncryptedWorkspaceKeyResponse{}, fmt.Errorf("CallGetEncryptedWorkspaceKey: Unsuccessful response: [response=%s]", response)
}

Expand All @@ -48,18 +48,18 @@ func CallGetServiceTokenDetailsV2(httpClient *resty.Client) (GetServiceTokenDeta
}

func CallGetSecretsV2(httpClient *resty.Client, request GetEncryptedSecretsV2Request) (GetEncryptedSecretsV2Response, error) {
var secretsResponse GetEncryptedSecretsV2Response = GetEncryptedSecretsV2Response{}
var encryptedSecretsResponse GetEncryptedSecretsV2Response
createHttpRequest := httpClient.
R().
SetResult(&secretsResponse.Secrets).
SetQueryParam("environment", request.EnvironmentName).
SetQueryParam("environment", request.Environment).
SetQueryParam("workspaceId", request.WorkspaceId).
SetResult(&encryptedSecretsResponse).
SetHeader("User-Agent", USER_AGENT_NAME)

if request.ETag != "" {
createHttpRequest.SetHeader("If-None-Match", request.ETag)
}
createHttpRequest.SetHeader("If-None-Match", request.ETag)

response, err := createHttpRequest.Get(fmt.Sprintf("%v/v2/secrets", API_HOST_URL))

response, err := createHttpRequest.Get(fmt.Sprintf("%v/v2/secret/workspace/%v", API_HOST_URL, request.WorkspaceId))
if err != nil {
return GetEncryptedSecretsV2Response{}, fmt.Errorf("CallGetSecretsV2: Unable to complete api request [err=%s]", err)
}
Expand All @@ -69,12 +69,12 @@ func CallGetSecretsV2(httpClient *resty.Client, request GetEncryptedSecretsV2Req
}

if response.StatusCode() == 304 {
secretsResponse.Modified = false
encryptedSecretsResponse.Modified = false
} else {
secretsResponse.Modified = true
encryptedSecretsResponse.Modified = true
}

secretsResponse.ETag = response.Header().Get("etag")
encryptedSecretsResponse.ETag = response.Header().Get("etag")

return secretsResponse, nil
return encryptedSecretsResponse, nil
}
44 changes: 18 additions & 26 deletions k8-operator/packages/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,28 @@ type GetEncryptedWorkspaceKeyResponse struct {
}

type GetEncryptedSecretsV2Request struct {
EnvironmentName string `json:"environmentName"`
WorkspaceId string `json:"workspaceId"`
ETag string `json:"etag,omitempty"`
Environment string `json:"environment"`
WorkspaceId string `json:"workspaceId"`
ETag string `json:"etag,omitempty"`
}

type GetEncryptedSecretsV2Response struct {
Secrets []struct {
ID string `json:"_id"`
Version int `json:"version"`
Workspace string `json:"workspace"`
Type string `json:"type"`
Environment string `json:"environment"`
SecretKeyCiphertext string `json:"secretKeyCiphertext"`
SecretKeyIV string `json:"secretKeyIV"`
SecretKeyTag string `json:"secretKeyTag"`
SecretKeyHash string `json:"secretKeyHash"`
SecretValueCiphertext string `json:"secretValueCiphertext"`
SecretValueIV string `json:"secretValueIV"`
SecretValueTag string `json:"secretValueTag"`
SecretValueHash string `json:"secretValueHash"`
SecretCommentCiphertext string `json:"secretCommentCiphertext"`
SecretCommentIV string `json:"secretCommentIV"`
SecretCommentTag string `json:"secretCommentTag"`
SecretCommentHash string `json:"secretCommentHash"`
V int `json:"__v"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
User string `json:"user,omitempty"`
}
ID string `json:"_id"`
Version int `json:"version"`
Workspace string `json:"workspace"`
Type string `json:"type"`
Environment string `json:"environment"`
SecretKeyCiphertext string `json:"secretKeyCiphertext"`
SecretKeyIV string `json:"secretKeyIV"`
SecretKeyTag string `json:"secretKeyTag"`
SecretValueCiphertext string `json:"secretValueCiphertext"`
SecretValueIV string `json:"secretValueIV"`
SecretValueTag string `json:"secretValueTag"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
User string `json:"user,omitempty"`
} `json:"secrets"`

Modified bool `json:"modified,omitempty"`
ETag string `json:"ETag,omitempty"`
Expand All @@ -68,7 +61,6 @@ type GetServiceTokenDetailsResponse struct {
Name string `json:"name"`
Workspace string `json:"workspace"`
Environment string `json:"environment"`
User string `json:"user"`
EncryptedKey string `json:"encryptedKey"`
Iv string `json:"iv"`
Tag string `json:"tag"`
Expand Down
7 changes: 4 additions & 3 deletions k8-operator/packages/util/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func GetPlainTextSecretsViaServiceToken(fullServiceToken string, etag string) ([
serviceToken := fmt.Sprintf("%v.%v.%v", serviceTokenParts[0], serviceTokenParts[1], serviceTokenParts[2])

httpClient := resty.New()

httpClient.SetAuthToken(serviceToken).
SetHeader("Accept", "application/json")

Expand All @@ -72,9 +73,9 @@ func GetPlainTextSecretsViaServiceToken(fullServiceToken string, etag string) ([
}

encryptedSecretsResponse, err := api.CallGetSecretsV2(httpClient, api.GetEncryptedSecretsV2Request{
WorkspaceId: serviceTokenDetails.Workspace,
EnvironmentName: serviceTokenDetails.Environment,
ETag: etag,
WorkspaceId: serviceTokenDetails.Workspace,
Environment: serviceTokenDetails.Environment,
ETag: etag,
})

if err != nil {
Expand Down

0 comments on commit 3ba636f

Please sign in to comment.