-
Notifications
You must be signed in to change notification settings - Fork 124
CSPL-542: Utility to get and update secret object #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e27e78c
ff0fdd0
1b3ce43
d845596
efbe7f2
4ceb9f5
86ef03d
988a73e
1542dca
8c4415d
48c0423
50226ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright (c) 2018-2021 Splunk Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testenv | ||
|
||
import ( | ||
b64 "encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
//SecretResponse Secret object struct | ||
type SecretResponse struct { | ||
HecToken string `json:"hec_token"` | ||
IdxcSecret string `json:"idxc_secret"` | ||
Pass4SymmKey string `json:"pass4SymmKey"` | ||
Password string `json:"password"` | ||
ShcSecret string `json:"shc_secret"` | ||
} | ||
|
||
// DecodeBase64 decodes base64 and returns string | ||
func DecodeBase64(str string) string { | ||
out, err := b64.StdEncoding.DecodeString(str) | ||
if err != nil { | ||
logf.Log.Error(err, "Failed to decode", "string", str) | ||
return "" | ||
} | ||
return string(out) | ||
} | ||
|
||
// EncodeBase64 Encodes base64 and returns string | ||
func EncodeBase64(str string) string { | ||
out := b64.StdEncoding.EncodeToString([]byte(str)) | ||
return out | ||
} | ||
|
||
// GetSecretObject Gets the secret object | ||
func GetSecretObject(deployment *Deployment, ns string) *SecretResponse { | ||
secretObjectName := fmt.Sprintf(SecretObject, ns) | ||
output, err := exec.Command("kubectl", "get", "secret", secretObjectName, "-n", ns, "-o", "jsonpath='{.data}'").Output() | ||
if err != nil { | ||
cmd := fmt.Sprintf("kubectl get secret %s -n %s -o jsonpath='{.data}'", secretObjectName, ns) | ||
logf.Log.Error(err, "Failed to execute command", "command", cmd) | ||
return nil | ||
} | ||
// Parse response into response struct | ||
restResponse := SecretResponse{} | ||
err = json.Unmarshal([]byte(strings.Trim(string(output), "'")), &restResponse) | ||
if err != nil { | ||
logf.Log.Error(err, "Failed to parse response") | ||
return nil | ||
} | ||
return &restResponse | ||
} | ||
|
||
// GetSecretKey Gets the value to specific key from secret object | ||
func GetSecretKey(deployment *Deployment, ns string, key string) string { | ||
restResponse := GetSecretObject(deployment, ns) | ||
//return key based on request | ||
switch key { | ||
case "hec_token": | ||
key := DecodeBase64(restResponse.HecToken) | ||
return key | ||
case "idxc_secret": | ||
key := DecodeBase64(restResponse.IdxcSecret) | ||
return key | ||
case "pass4SymmKey": | ||
key := DecodeBase64(restResponse.Pass4SymmKey) | ||
return key | ||
case "password": | ||
key := DecodeBase64(restResponse.Password) | ||
return key | ||
case "shc_secret": | ||
key := DecodeBase64(restResponse.ShcSecret) | ||
return key | ||
default: | ||
return "Invalid Key" | ||
} | ||
} | ||
|
||
//ModifySecretObject Modifies the entire secret object | ||
func ModifySecretObject(deployment *Deployment, data map[string][]byte, ns string) bool { | ||
secretName := fmt.Sprintf(SecretObject, ns) | ||
secret := newSecretSpec(ns, secretName, data) | ||
//Update object using spec | ||
err := deployment.updateCR(secret) | ||
if err != nil { | ||
logf.Log.Error(err, "Unable to update secret object") | ||
return false | ||
} | ||
return true | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: extra line unnecessary here? |
||
} | ||
|
||
//ModifySecretKey Modifies the specific key in secret object | ||
func ModifySecretKey(deployment *Deployment, ns string, key string, value string) bool { | ||
//Get current config for update | ||
restResponse := GetSecretObject(deployment, ns) | ||
out, err := json.Marshal(restResponse) | ||
if err != nil { | ||
logf.Log.Error(err, "Failed to parse response") | ||
return false | ||
} | ||
|
||
//Convert object to map for update | ||
var data map[string][]byte | ||
err = json.Unmarshal([]byte(out), &data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious, how does the unmarshal convert the variable out to a map? And what data type is out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out is a Json object. Unmarshal will convert it to a map. Tested it out when running the local changes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the input to unmarshal need to be of a certain type for it to convert it to a map? i.e []byte? How does it determine the key and value pairs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the object is initially built into a json out of the exec.command() function using the struct defined at the top. Here we take care of any extra fields that might have come accidentally. Post that its simple to convert from json to map and then to update |
||
if err != nil { | ||
logf.Log.Error(err, "Failed to parse response") | ||
return false | ||
} | ||
|
||
//Modify data | ||
data[key] = []byte(EncodeBase64(value)) | ||
modify := ModifySecretObject(deployment, data, ns) | ||
return modify | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,9 @@ const ( | |
|
||
// MonitoringConsolePod Montioring Console Statefulset Template | ||
MonitoringConsolePod = "splunk-%s-monitoring-console-%d" | ||
|
||
// SecretObject Secret object Template | ||
SecretObject = "splunk-%s-secret" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Change to SecretObjectName ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use the secretObjectName in the function itself after i fill in the 'namespace'. Think it would be ok to keep as it is |
||
) | ||
|
||
var ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does updateCR take care of a case where secret object is not present?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. If the object is not present. It creates the object with the details specified
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if it does exist, does it update? If yes we are good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. This fcn is mainly to update it. But does take care of the scenario when object is not present