Skip to content

Commit

Permalink
Require different static token for using fleet-manager's Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
mtesseract committed Aug 9, 2022
1 parent ee13acd commit 40ed76b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions .openshift-ci/tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ if [[ "${OPENSHIFT_CI:-}" == "true" ]]; then
export "${secret_name}"="${secret_value}"
done
export STATIC_TOKEN="${FLEET_STATIC_TOKEN:-}"
export STATIC_TOKEN_ADMIN="${FLEET_STATIC_TOKEN_ADMIN:-}"
export QUAY_USER="${IMAGE_PUSH_USERNAME:-}"
export QUAY_TOKEN="${IMAGE_PUSH_PASSWORD:-}"
export CLUSTER_TYPE="openshift-ci"
Expand Down
5 changes: 4 additions & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
openshiftRouteV1 "github.com/openshift/api/route/v1"
"github.com/stackrox/acs-fleet-manager/e2e/envtokenauth"
"github.com/stackrox/acs-fleet-manager/fleetshard/pkg/fleetmanager"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/constants"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/api/public"
Expand Down Expand Up @@ -66,7 +67,9 @@ var _ = Describe("Central", func() {
client, err = fleetmanager.NewClient(fleetManagerEndpoint, "cluster-id", auth)
Expect(err).ToNot(HaveOccurred())

adminClient, err = NewAdminClient(fleetManagerEndpoint, auth)
adminAuth, err := envtokenauth.CreateAuth("STATIC_TOKEN_ADMIN")
Expect(err).ToNot(HaveOccurred())
adminClient, err = NewAdminClient(fleetManagerEndpoint, adminAuth)
Expect(err).ToNot(HaveOccurred())

})
Expand Down
33 changes: 33 additions & 0 deletions e2e/envtokenauth/auth_env_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package envtokenauth

import (
"fmt"
"net/http"
"os"

"github.com/stackrox/acs-fleet-manager/fleetshard/pkg/fleetmanager"
)

// Implements the Auth interface for simple static token based authentication
// while fetching the token from a custom environment variable.
type envTokenAuth struct {
token string
}

// CreateAuth creates a new Auth instance which implements static token authentication
// while fetching the token from the environment using the specified environment variable name.
func CreateAuth(name string) (fleetmanager.Auth, error) {
token := os.Getenv(name)
if token == "" {
return nil, fmt.Errorf("no token named %q found in current environment", name)
}
return &envTokenAuth{
token: token,
}, nil
}

// AddAuth adds an Authorization header to the provided HTTP request.
func (a *envTokenAuth) AddAuth(req *http.Request) error {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.token))
return nil
}

0 comments on commit 40ed76b

Please sign in to comment.