Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- (Bugfix) Allow ArangoBackup Creation during Upload state
- (Hotfix) Fix `ArangoDeployment` SubResource in CRD auto-installer
- (Bugfix) Fix Operator Logger NPE
- (Bugfix) Fix License RAW value discovery

## [1.2.13](https://github.com/arangodb/kube-arangodb/tree/1.2.13) (2022-06-07)
- (Bugfix) Fix arangosync members state inspection
Expand Down
13 changes: 9 additions & 4 deletions pkg/util/k8sutil/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ func GetLicenseFromSecret(secret secret.Inspector, name string) (LicenseSecret,
}

if v1, ok1 := s.Data[constants.SecretKeyV2License]; ok1 {
l.V2 = License(v1)
// some customers put the raw JSON-encoded value, but operator and DB servers expect the base64-encoded value
if isJSONBytes(v1) {
l.V2 = License(base64.StdEncoding.EncodeToString(v1))
} else {
l.V2 = License(v1)
}
} else if v2, ok2 := s.Data[constants.SecretKeyV2Token]; ok2 {
licenseV2 := v2
// some customers put the raw JSON-encoded value, but operator and DB servers expect the base64-encoded value
if isJSONBytes(v2) {
base64.StdEncoding.Encode(v2, licenseV2)
l.V2 = License(base64.StdEncoding.EncodeToString(v2))
} else {
l.V2 = License(v2)
}
l.V2 = License(licenseV2)
}

return l, true
Expand Down
137 changes: 137 additions & 0 deletions pkg/util/k8sutil/license_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package k8sutil

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"testing"

"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
"github.com/arangodb/kube-arangodb/pkg/util/tests"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
)

func encodeLicenseKey(in string) string {
return base64.StdEncoding.EncodeToString([]byte(in))
}

func createLicenseSecret(t *testing.T, c kclient.Client, key, value string) string {
s := fmt.Sprintf("secret-%s", uuid.NewUUID())

q := &core.Secret{
ObjectMeta: meta.ObjectMeta{
Name: s,
Namespace: tests.FakeNamespace,
},
Data: map[string][]byte{
key: []byte(value),
},
}

_, err := c.Kubernetes().CoreV1().Secrets(tests.FakeNamespace).Create(context.Background(), q, meta.CreateOptions{})
require.NoError(t, err)

return s
}

func generateLK(t *testing.T) string {
z := map[string]string{
"grant": "",
"signature": "",
}

i, err := json.Marshal(z)

require.NoError(t, err)

return string(i)
}

func Test_GetLicenseFromSecret(t *testing.T) {
lk := generateLK(t)
lke := encodeLicenseKey(lk)

c := kclient.NewFakeClient()
i := tests.NewInspector(t, c)

t.Run(constants.SecretKeyV2License, func(t *testing.T) {
t.Run("Encoded license", func(t *testing.T) {
n := createLicenseSecret(t, c, constants.SecretKeyV2License, lke)

require.NoError(t, i.Refresh(context.Background()))

license, ok := GetLicenseFromSecret(i, n)
require.True(t, ok)

require.Empty(t, license.V1)
require.NotEmpty(t, license.V2)
require.EqualValues(t, lke, license.V2)
})

t.Run("Raw license", func(t *testing.T) {
n := createLicenseSecret(t, c, constants.SecretKeyV2License, lk)

require.NoError(t, i.Refresh(context.Background()))

license, ok := GetLicenseFromSecret(i, n)
require.True(t, ok)

require.Empty(t, license.V1)
require.NotEmpty(t, license.V2)
require.EqualValues(t, lke, license.V2)
})
})

t.Run(constants.SecretKeyV2Token, func(t *testing.T) {
t.Run("Encoded license", func(t *testing.T) {
n := createLicenseSecret(t, c, constants.SecretKeyV2Token, lke)

require.NoError(t, i.Refresh(context.Background()))

license, ok := GetLicenseFromSecret(i, n)
require.True(t, ok)

require.Empty(t, license.V1)
require.NotEmpty(t, license.V2)
require.EqualValues(t, lke, license.V2)
})

t.Run("Raw license", func(t *testing.T) {
n := createLicenseSecret(t, c, constants.SecretKeyV2Token, lk)

require.NoError(t, i.Refresh(context.Background()))

license, ok := GetLicenseFromSecret(i, n)
require.True(t, ok)

require.Empty(t, license.V1)
require.NotEmpty(t, license.V2)
require.EqualValues(t, lke, license.V2)
})
})
}