Skip to content

Commit e5e9186

Browse files
authored
[Bugfix] Fix License RAW value discovery (#1012)
1 parent 6c57529 commit e5e9186

File tree

3 files changed

+147
-4
lines changed

3 files changed

+147
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- (Bugfix) Allow ArangoBackup Creation during Upload state
1616
- (Hotfix) Fix `ArangoDeployment` SubResource in CRD auto-installer
1717
- (Bugfix) Fix Operator Logger NPE
18+
- (Bugfix) Fix License RAW value discovery
1819

1920
## [1.2.13](https://github.com/arangodb/kube-arangodb/tree/1.2.13) (2022-06-07)
2021
- (Bugfix) Fix arangosync members state inspection

pkg/util/k8sutil/license.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,19 @@ func GetLicenseFromSecret(secret secret.Inspector, name string) (LicenseSecret,
5757
}
5858

5959
if v1, ok1 := s.Data[constants.SecretKeyV2License]; ok1 {
60-
l.V2 = License(v1)
60+
// some customers put the raw JSON-encoded value, but operator and DB servers expect the base64-encoded value
61+
if isJSONBytes(v1) {
62+
l.V2 = License(base64.StdEncoding.EncodeToString(v1))
63+
} else {
64+
l.V2 = License(v1)
65+
}
6166
} else if v2, ok2 := s.Data[constants.SecretKeyV2Token]; ok2 {
62-
licenseV2 := v2
6367
// some customers put the raw JSON-encoded value, but operator and DB servers expect the base64-encoded value
6468
if isJSONBytes(v2) {
65-
base64.StdEncoding.Encode(v2, licenseV2)
69+
l.V2 = License(base64.StdEncoding.EncodeToString(v2))
70+
} else {
71+
l.V2 = License(v2)
6672
}
67-
l.V2 = License(licenseV2)
6873
}
6974

7075
return l, true

pkg/util/k8sutil/license_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)