зеркало из https://github.com/Azure/ARO-RP.git
Add Unmarshal Secret
Introduce helper function to unmarshal pull-secret data. Signed-off-by: Petr Kotas <pkotas@redhat.com>
This commit is contained in:
Родитель
b80e6b5e7c
Коммит
e86703b94d
|
@ -9,6 +9,8 @@ import (
|
|||
"os"
|
||||
"reflect"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
"github.com/Azure/ARO-RP/pkg/api"
|
||||
)
|
||||
|
||||
|
@ -16,6 +18,31 @@ type pullSecret struct {
|
|||
Auths map[string]map[string]interface{} `json:"auths,omitempty"`
|
||||
}
|
||||
|
||||
// Unmarshal pull-secret data which is stored in corev1.Secret.Data
|
||||
// the data has form:
|
||||
// {"auths": {"secret key": {"auth": "secret value"}, "secret key": {"auth": "secret value"}}}
|
||||
//
|
||||
// returns map of extracted secrets in a form:
|
||||
// {"secret key": "secret value"}
|
||||
// error is returned when parsing fails
|
||||
func UnmarshalSecretData(ps *corev1.Secret) (map[string]string, error) {
|
||||
var pullSecretData pullSecret
|
||||
if ps != nil {
|
||||
if data := ps.Data[corev1.DockerConfigJsonKey]; len(data) > 0 {
|
||||
if err := json.Unmarshal(data, &pullSecretData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
secretData := map[string]string{}
|
||||
for k, v := range pullSecretData.Auths {
|
||||
secretData[k] = v["auth"].(string)
|
||||
}
|
||||
|
||||
return secretData, nil
|
||||
}
|
||||
|
||||
func SetRegistryProfiles(_ps string, rps ...*api.RegistryProfile) (string, bool, error) {
|
||||
if _ps == "" {
|
||||
_ps = "{}"
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
|
||||
"github.com/Azure/ARO-RP/pkg/api"
|
||||
)
|
||||
|
||||
|
@ -316,3 +318,47 @@ func TestMerge(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalSecretData(t *testing.T) {
|
||||
test := []struct {
|
||||
name string
|
||||
ps *corev1.Secret
|
||||
wantAuth map[string]string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "ok secret",
|
||||
ps: &corev1.Secret{
|
||||
Data: map[string][]byte{
|
||||
corev1.DockerConfigJsonKey: []byte(`{"auths":{"arosvc.azurecr.io":{"auth":"ZnJlZDplbnRlcg=="}, "registry.redhat.io":{"auth":"ZnJlZDplbnRlcg=="}}}`),
|
||||
},
|
||||
},
|
||||
wantAuth: map[string]string{
|
||||
"arosvc.azurecr.io": "ZnJlZDplbnRlcg==",
|
||||
"registry.redhat.io": "ZnJlZDplbnRlcg==",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "broken secret",
|
||||
ps: &corev1.Secret{
|
||||
Data: map[string][]byte{
|
||||
corev1.DockerConfigJsonKey: []byte(`{"auths":"arosvc.azurecr.io":{"auth":"ZnJlZDplbnRlcg=="}}}`),
|
||||
},
|
||||
},
|
||||
wantErr: "invalid character ':' after object key:value pair",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range test {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
out, err := UnmarshalSecretData(tt.ps)
|
||||
if err != nil {
|
||||
if err.Error() != tt.wantErr {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
} else if !reflect.DeepEqual(out, tt.wantAuth) {
|
||||
t.Fatalf("Auth does not match:\n%v\n%v", out, tt.wantAuth)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче