Skip to content

Commit

Permalink
feat(ISV-5130): move secret creation to releaseLib
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Jediny <[email protected]>
  • Loading branch information
jedinym committed Nov 13, 2024
1 parent 34e70b9 commit 72b1e1f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 49 deletions.
51 changes: 2 additions & 49 deletions tests/release/pipelines/rh_advisories.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package pipelines

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"regexp"
"time"

Expand All @@ -15,15 +13,12 @@ import (
releaseapi "github.com/konflux-ci/release-service/api/v1alpha1"
tektonutils "github.com/konflux-ci/release-service/tekton/utils"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/devfile/library/v2/pkg/util"
"github.com/konflux-ci/e2e-tests/pkg/constants"
"github.com/konflux-ci/e2e-tests/pkg/framework"
"github.com/konflux-ci/e2e-tests/pkg/utils"
"github.com/konflux-ci/e2e-tests/pkg/utils/tekton"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"knative.dev/pkg/apis"

Expand Down Expand Up @@ -73,13 +68,13 @@ var _ = framework.ReleasePipelinesSuiteDescribe("e2e tests for rh-advisories pip
"key": constants.PYXIS_STAGE_KEY_ENV,
"cert": constants.PYXIS_STAGE_CERT_ENV,
}
createSecretFromEnv(managedFw, managedNamespace, "pyxis", pyxisFieldEnvMap)
releasecommon.CreateOpaqueSecret(managedFw, managedNamespace, "pyxis", pyxisFieldEnvMap)

atlasFieldEnvMap := map[string]string{
"sso_account": constants.ATLAS_STAGE_ACCOUNT_ENV,
"sso_token": constants.ATLAS_STAGE_TOKEN_ENV,
}
createSecretFromEnv(managedFw, managedNamespace, "atlas", atlasFieldEnvMap)
releasecommon.CreateOpaqueSecret(managedFw, managedNamespace, "atlas", atlasFieldEnvMap)

err = managedFw.AsKubeAdmin.CommonController.LinkSecretToServiceAccount(managedNamespace, releasecommon.RedhatAppstudioUserSecret, constants.DefaultPipelineServiceAccount, true)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -264,45 +259,3 @@ func createADVSReleasePlanAdmission(advsRPAName string, managedFw framework.Fram
})
Expect(err).NotTo(HaveOccurred())
}

// createSecretfromEnv creates a k8s Secret in the managed workspace if it
// doesn't exist. It populates the Secret data fields based on the mapping of
// fields to environment variables containing the base64 encoded field data.
func createSecretFromEnv(
managedFw *framework.Framework,
managedNamespace, secretName string,
fieldEnvMap map[string]string,
) {
secretData := make(map[string][]byte)

for field, envVar := range fieldEnvMap {
envValue := os.Getenv(envVar)
Expect(envValue).ToNot(BeEmpty())

decodedValue, err := base64.StdEncoding.DecodeString(envValue)
Expect(err).ToNot(HaveOccurred())

secretData[field] = decodedValue
}

secret, err := managedFw.AsKubeAdmin.CommonController.GetSecret(
managedNamespace,
secretName,
)
if secret == nil || errors.IsNotFound(err) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: managedNamespace,
},
Type: corev1.SecretTypeOpaque,
Data: secretData,
}

_, err = managedFw.AsKubeAdmin.CommonController.CreateSecret(
managedNamespace,
secret,
)
Expect(err).ToNot(HaveOccurred())
}
}
44 changes: 44 additions & 0 deletions tests/release/releaseLib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"fmt"
"os"
"time"
"encoding/base64"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appservice "github.com/konflux-ci/application-api/api/v1alpha1"
appstudioApi "github.com/konflux-ci/application-api/api/v1alpha1"
"github.com/devfile/library/v2/pkg/util"
"github.com/konflux-ci/e2e-tests/pkg/constants"
"github.com/konflux-ci/e2e-tests/pkg/framework"
"github.com/konflux-ci/e2e-tests/pkg/utils"
"k8s.io/apimachinery/pkg/api/errors"
releaseApi "github.com/konflux-ci/release-service/api/v1alpha1"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -136,4 +140,44 @@ func CheckReleaseStatus(releaseCR *releaseApi.Release) (error) {
return nil
}

// CreateOpaqueSecret creates a k8s Secret in the managed workspace if it
// doesn't exist. It populates the Secret data fields based on the mapping of
// fields to environment variables containing the base64 encoded field data.
func CreateOpaqueSecret(
fw *framework.Framework,
namespace, secretName string,
fieldEnvMap map[string]string,
) {
secretData := make(map[string][]byte)

for field, envVar := range fieldEnvMap {
envValue := os.Getenv(envVar)
Expect(envValue).ToNot(BeEmpty())

decodedValue, err := base64.StdEncoding.DecodeString(envValue)
Expect(err).ToNot(HaveOccurred())

secretData[field] = decodedValue
}

secret, err := fw.AsKubeAdmin.CommonController.GetSecret(
namespace,
secretName,
)
if secret == nil || errors.IsNotFound(err) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: namespace,
},
Type: corev1.SecretTypeOpaque,
Data: secretData,
}

_, err = fw.AsKubeAdmin.CommonController.CreateSecret(
namespace,
secret,
)
Expect(err).ToNot(HaveOccurred())
}
}

0 comments on commit 72b1e1f

Please sign in to comment.