github.com/splunk/dan1-qbec@v0.7.3/internal/types/secrets_test.go (about)

     1  /*
     2     Copyright 2019 Splunk Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"encoding/base64"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/ghodss/yaml"
    25  	"github.com/splunk/qbec/internal/model"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  var cm = `
    30  ---
    31  apiVersion:  v1
    32  kind: ConfigMap
    33  metadata:
    34    namespace: ns1
    35    name: cm
    36  data:
    37    foo: bar
    38  `
    39  
    40  var b64 = base64.StdEncoding.EncodeToString([]byte("changeme"))
    41  
    42  var secret = fmt.Sprintf(`
    43  ---
    44  apiVersion:  v1
    45  kind: Secret
    46  metadata:
    47    namespace: ns1
    48    name: s
    49  data:
    50    foo: %s
    51  `, b64)
    52  
    53  func toData(s string) map[string]interface{} {
    54  	var data map[string]interface{}
    55  	err := yaml.Unmarshal([]byte(s), &data)
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	return data
    60  }
    61  
    62  func TestSecrets(t *testing.T) {
    63  	cmObj := model.NewK8sLocalObject(toData(cm), "app1", "", "c1", "e1")
    64  	secretObj := model.NewK8sLocalObject(toData(secret), "", "app1", "c1", "e1")
    65  	a := assert.New(t)
    66  	a.False(HasSensitiveInfo(cmObj.ToUnstructured()))
    67  	a.True(HasSensitiveInfo(secretObj.ToUnstructured()))
    68  	changed, ok := HideSensitiveLocalInfo(cmObj)
    69  	a.Equal(cmObj, changed)
    70  	a.False(ok)
    71  	changed, ok = HideSensitiveLocalInfo(secretObj)
    72  	a.NotEqual(secretObj, changed)
    73  	a.True(ok)
    74  	v := changed.ToUnstructured().Object["data"].(map[string]interface{})["foo"]
    75  	a.NotEqual(b64, v)
    76  }