|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package k8sutil |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "encoding/base64" |
| 26 | + "encoding/json" |
| 27 | + "fmt" |
| 28 | + "testing" |
| 29 | + |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/kclient" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/util/tests" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | + core "k8s.io/api/core/v1" |
| 35 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 37 | +) |
| 38 | + |
| 39 | +func encodeLicenseKey(in string) string { |
| 40 | + return base64.StdEncoding.EncodeToString([]byte(in)) |
| 41 | +} |
| 42 | + |
| 43 | +func createLicenseSecret(t *testing.T, c kclient.Client, key, value string) string { |
| 44 | + s := fmt.Sprintf("secret-%s", uuid.NewUUID()) |
| 45 | + |
| 46 | + q := &core.Secret{ |
| 47 | + ObjectMeta: meta.ObjectMeta{ |
| 48 | + Name: s, |
| 49 | + Namespace: tests.FakeNamespace, |
| 50 | + }, |
| 51 | + Data: map[string][]byte{ |
| 52 | + key: []byte(value), |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + _, err := c.Kubernetes().CoreV1().Secrets(tests.FakeNamespace).Create(context.Background(), q, meta.CreateOptions{}) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + return s |
| 60 | +} |
| 61 | + |
| 62 | +func generateLK(t *testing.T) string { |
| 63 | + z := map[string]string{ |
| 64 | + "grant": "", |
| 65 | + "signature": "", |
| 66 | + } |
| 67 | + |
| 68 | + i, err := json.Marshal(z) |
| 69 | + |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + return string(i) |
| 73 | +} |
| 74 | + |
| 75 | +func Test_GetLicenseFromSecret(t *testing.T) { |
| 76 | + lk := generateLK(t) |
| 77 | + lke := encodeLicenseKey(lk) |
| 78 | + |
| 79 | + c := kclient.NewFakeClient() |
| 80 | + i := tests.NewInspector(t, c) |
| 81 | + |
| 82 | + t.Run(constants.SecretKeyV2License, func(t *testing.T) { |
| 83 | + t.Run("Encoded license", func(t *testing.T) { |
| 84 | + n := createLicenseSecret(t, c, constants.SecretKeyV2License, lke) |
| 85 | + |
| 86 | + require.NoError(t, i.Refresh(context.Background())) |
| 87 | + |
| 88 | + license, ok := GetLicenseFromSecret(i, n) |
| 89 | + require.True(t, ok) |
| 90 | + |
| 91 | + require.Empty(t, license.V1) |
| 92 | + require.NotEmpty(t, license.V2) |
| 93 | + require.EqualValues(t, lke, license.V2) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("Raw license", func(t *testing.T) { |
| 97 | + n := createLicenseSecret(t, c, constants.SecretKeyV2License, lk) |
| 98 | + |
| 99 | + require.NoError(t, i.Refresh(context.Background())) |
| 100 | + |
| 101 | + license, ok := GetLicenseFromSecret(i, n) |
| 102 | + require.True(t, ok) |
| 103 | + |
| 104 | + require.Empty(t, license.V1) |
| 105 | + require.NotEmpty(t, license.V2) |
| 106 | + require.EqualValues(t, lke, license.V2) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run(constants.SecretKeyV2Token, func(t *testing.T) { |
| 111 | + t.Run("Encoded license", func(t *testing.T) { |
| 112 | + n := createLicenseSecret(t, c, constants.SecretKeyV2Token, lke) |
| 113 | + |
| 114 | + require.NoError(t, i.Refresh(context.Background())) |
| 115 | + |
| 116 | + license, ok := GetLicenseFromSecret(i, n) |
| 117 | + require.True(t, ok) |
| 118 | + |
| 119 | + require.Empty(t, license.V1) |
| 120 | + require.NotEmpty(t, license.V2) |
| 121 | + require.EqualValues(t, lke, license.V2) |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("Raw license", func(t *testing.T) { |
| 125 | + n := createLicenseSecret(t, c, constants.SecretKeyV2Token, lk) |
| 126 | + |
| 127 | + require.NoError(t, i.Refresh(context.Background())) |
| 128 | + |
| 129 | + license, ok := GetLicenseFromSecret(i, n) |
| 130 | + require.True(t, ok) |
| 131 | + |
| 132 | + require.Empty(t, license.V1) |
| 133 | + require.NotEmpty(t, license.V2) |
| 134 | + require.EqualValues(t, lke, license.V2) |
| 135 | + }) |
| 136 | + }) |
| 137 | +} |
0 commit comments