Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
a59724b
chore(deps): update module github.com/onsi/gomega to v1.22.1
renovate[bot] Oct 11, 2022
c9ff25b
chore(deps): update cimg/go docker tag to v1.19.2
renovate[bot] Oct 10, 2022
79757d0
fix: add base href for k3d uri
a-cordier Oct 13, 2022
59b42b5
chore(release): 0.1.0-alpha.6 [skip ci]
graviteeio Oct 14, 2022
f2b16f0
chore(deps): update gravitee orb to v2.1.16
renovate[bot] Oct 17, 2022
8a20fcf
chore(deps): update kubernetes packages to v0.25.3
renovate[bot] Oct 17, 2022
2278a16
fix: rename cors fields to match v3 definition
a-cordier Oct 14, 2022
8be05d8
chore(release): 0.1.0-alpha.7 [skip ci]
graviteeio Oct 17, 2022
19e9dd3
fix: merge create and update of api definition
a-cordier Oct 18, 2022
83bb2c5
refactor: rename status generation
a-cordier Oct 20, 2022
b309629
refactor: rename add finalizer method
a-cordier Oct 20, 2022
78ed8e2
refactor: rename has finalizer method
a-cordier Oct 20, 2022
7f68a69
chore(release): 0.1.0-alpha.8 [skip ci]
graviteeio Oct 20, 2022
400c160
refactor: rename has management context method
a-cordier Oct 21, 2022
9c69558
chore(deps): update mcr.microsoft.com/azure-cli docker tag to v2.41.0
renovate[bot] Oct 24, 2022
97901ed
chore(deps): update module github.com/onsi/ginkgo/v2 to v2.4.0
renovate[bot] Oct 24, 2022
7427457
fix: import api with life cycle state
Okhelifi Oct 25, 2022
3b98614
test: add context ref on update
a-cordier Oct 21, 2022
b75aec8
chore(release): 0.1.0-alpha.9 [skip ci]
graviteeio Oct 25, 2022
933af8d
ci: remove wait step from test job
a-cordier Oct 21, 2022
c73cc63
refactor: error handling
a-cordier Oct 21, 2022
dd74bba
refactor: move integration tests to their own package
a-cordier Oct 27, 2022
43276ce
fix: reconcile api resources on context updates
a-cordier Oct 27, 2022
8f4e099
chore(release): 0.1.0-alpha.10 [skip ci]
graviteeio Oct 27, 2022
bbe475a
fix: add enabled in health check model
Okhelifi Oct 27, 2022
71346fa
fix: change fail over data type
Okhelifi Oct 27, 2022
565d745
fix: align endpoint mapping with apim
Okhelifi Oct 27, 2022
62323fb
docs: update reference
Okhelifi Oct 28, 2022
e7f1083
chore(release): 0.1.0-alpha.11 [skip ci]
graviteeio Oct 28, 2022
b54d9a7
fix: reference secret in context
a-cordier Oct 27, 2022
39c682f
refactor: move test fixtures and const to internal
a-cordier Oct 28, 2022
8a94424
refactor: define fixture paths as constants
a-cordier Oct 28, 2022
3907f83
refactor: default secret namespace to context one
a-cordier Oct 28, 2022
0ac9d4b
chore(release): 0.1.0-alpha.12 [skip ci]
graviteeio Oct 28, 2022
3aebf3e
chore(deps): update module github.com/onsi/gomega to v1.23.0
renovate[bot] Oct 31, 2022
814794e
ci: update go version for machine executor to 1.19.2
a-cordier Oct 31, 2022
4bd7456
fix: add rbac marker for secret lists
a-cordier Nov 2, 2022
b3c431b
chore(release): 0.1.0-alpha.13 [skip ci]
graviteeio Nov 2, 2022
34a7b91
refactor(tests): make context names random
a-cordier Oct 28, 2022
81933cb
test: delete all resources at once after suite
a-cordier Oct 29, 2022
1b29bd3
refactor(test): move unit tests to ginkgo
a-cordier Nov 1, 2022
111cc3e
ci: run tests with ginkgo cli
a-cordier Nov 1, 2022
784abbe
refactor: log bad request error messages
a-cordier Oct 31, 2022
c4032eb
fix: import api with several endpoint groups
a-cordier Oct 31, 2022
a874ac6
fix: import api with disabled health check
a-cordier Nov 2, 2022
7179561
fix: import API with logging
a-cordier Nov 2, 2022
28c7acc
test: add table test with featured api definitions
a-cordier Nov 2, 2022
6e81619
chore: clean up config sample files
a-cordier Nov 2, 2022
0afeb80
chore(release): 0.1.0-alpha.14 [skip ci]
graviteeio Nov 3, 2022
4e45fbc
ci: fix ginkgo flags
a-cordier Nov 4, 2022
9f12dff
test: add create test for api with metadata
a-cordier Nov 4, 2022
9867084
test: add opt in option to run the operator locally
a-cordier Nov 3, 2022
ddc6af5
test: update management context when running in cluster
a-cordier Nov 3, 2022
1392143
chore: generate management context webhook
a-cordier Nov 4, 2022
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
Prev Previous commit
Next Next commit
fix: reference secret in context
  • Loading branch information
a-cordier committed Oct 28, 2022
commit b54d9a721d28002c83fe2b72d8bbe04acfc87cba
27 changes: 4 additions & 23 deletions api/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
// +kubebuilder:object:generate=true
package model

import (
"net/http"
)

type ContextRef struct {
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Expand All @@ -38,6 +34,7 @@ type Context struct {
type Auth struct {
BearerToken string `json:"bearerToken,omitempty"`
Credentials *BasicAuth `json:"credentials,omitempty"`
SecretRef *SecretRef `json:"secretRef,omitempty"`
}

type BasicAuth struct {
Expand All @@ -47,23 +44,7 @@ type BasicAuth struct {
Password string `json:"password,omitempty"`
}

func (ctx Context) Authenticate(req *http.Request) {
if ctx.Auth == nil {
return
}

bearerToken := ctx.Auth.BearerToken
if bearerToken != "" {
req.Header.Add("Authorization", "Bearer "+bearerToken)
} else if ctx.Auth.Credentials != nil {
username := ctx.Auth.Credentials.Username
password := ctx.Auth.Credentials.Password
setBasicAuth(req, username, password)
}
}

func setBasicAuth(request *http.Request, username, password string) {
if username != "" {
request.SetBasicAuth(username, password)
}
type SecretRef struct {
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
}
20 changes: 20 additions & 0 deletions api/model/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions api/v1alpha1/managementcontext_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package v1alpha1

import (
"net/http"

"github.com/gravitee-io/gravitee-kubernetes-operator/api/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -57,3 +59,42 @@ type ManagementContextList struct {
func init() {
SchemeBuilder.Register(&ManagementContext{}, &ManagementContextList{})
}

func (ctx *ManagementContext) HasAuthentication() bool {
return ctx.Spec.Auth != nil
}

func (ctx *ManagementContext) HasSecretRef() bool {
if !ctx.HasAuthentication() {
return false
}

return ctx.Spec.Auth.SecretRef != nil
}

func (ctx *ManagementContext) Authenticate(req *http.Request) {
if !ctx.HasAuthentication() {
return
}

bearerToken := ctx.Spec.Auth.BearerToken
basicAuth := ctx.Spec.Auth.Credentials

if bearerToken != "" {
setBearerToken(req, bearerToken)
} else if basicAuth != nil {
setBasicAuth(req, basicAuth)
}
}

func setBearerToken(request *http.Request, token string) {
if token != "" {
request.Header.Add("Authorization", "Bearer "+token)
}
}

func setBasicAuth(request *http.Request, auth *model.BasicAuth) {
if auth != nil && auth.Username != "" {
request.SetBasicAuth(auth.Username, auth.Password)
}
}
9 changes: 9 additions & 0 deletions config/crd/bases/gravitee.io_managementcontexts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ spec:
username:
type: string
type: object
secretRef:
properties:
name:
type: string
namespace:
type: string
required:
- name
type: object
type: object
baseUrl:
pattern: ^http(s?):\/\/.+$
Expand Down
28 changes: 28 additions & 0 deletions config/samples/context/dev/managementcontext_secretRef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (C) 2015 The Gravitee team (http://gravitee.io)
#
# 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.

# Use this context if you are running APIM in K3d and GKO locally
apiVersion: gravitee.io/v1alpha1
kind: ManagementContext
metadata:
name: dev-mgmt-ctx
namespace: default
spec:
baseUrl: http://localhost:9000
environmentId: DEFAULT
organizationId: DEFAULT
auth:
secretRef:
name: apim-context-credentials
namespace: apim-dev
4 changes: 2 additions & 2 deletions controllers/apim/apidefinition/apidefinition_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/go-logr/logr"
gio "github.com/gravitee-io/gravitee-kubernetes-operator/api/v1alpha1"
"github.com/gravitee-io/gravitee-kubernetes-operator/controllers/apim/apidefinition/internal"
"github.com/gravitee-io/gravitee-kubernetes-operator/controllers/apim/managementcontext"
"github.com/gravitee-io/gravitee-kubernetes-operator/internal/utils"

"github.com/gravitee-io/gravitee-kubernetes-operator/pkg/keys"
Expand Down Expand Up @@ -91,7 +90,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
event := utils.NewEvent(r.Recorder)

if apiDefinition.Spec.Context != nil {
managementContext, ctxErr := managementcontext.Get(ctx, r.Client, log, apiDefinition.Spec.Context)
managementContext, ctxErr := apisDelegate.ResolveContext(apiDefinition.Spec.Context)

if ctxErr != nil {
log.Error(ctxErr, "And error has occurred while trying to retrieve ManagementContext")
event.NormalEvent(
Expand Down
64 changes: 64 additions & 0 deletions controllers/apim/apidefinition/internal/management_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (C) 2015 The Gravitee team (http://gravitee.io)
//
// 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.

package internal

import (
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model"
gio "github.com/gravitee-io/gravitee-kubernetes-operator/api/v1alpha1"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
bearerTokenSecretKey = "bearerToken"
usernameSecretKey = "username"
passwordSecretKey = "password"
)

func (d *Delegate) ResolveContext(
contextRef *model.ContextRef,
) (*gio.ManagementContext, error) {
apimContext := new(gio.ManagementContext)
ns := types.NamespacedName{Name: contextRef.Name, Namespace: contextRef.Namespace}

d.log.Info("Looking for context from", "namespace", contextRef.Namespace, "name", contextRef.Name)

if err := d.k8sClient.Get(d.ctx, ns, apimContext); err != nil {
return nil, err
}

if apimContext.HasSecretRef() {
secret := new(coreV1.Secret)
secretName := apimContext.Spec.Auth.SecretRef.Name
secretNameSpace := apimContext.Spec.Auth.SecretRef.Namespace
secretKey := types.NamespacedName{Name: secretName, Namespace: secretNameSpace}

if err := d.k8sClient.Get(d.ctx, secretKey, secret); err != nil {
return nil, err
}

bearerToken := string(secret.Data[bearerTokenSecretKey])
username := string(secret.Data[usernameSecretKey])
password := string(secret.Data[passwordSecretKey])

apimContext.Spec.Auth.BearerToken = bearerToken
apimContext.Spec.Auth.Credentials = &model.BasicAuth{
Username: username,
Password: password,
}
}

return apimContext, nil
}
29 changes: 24 additions & 5 deletions controllers/apim/managementcontext/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,49 @@ package managementcontext

import (
"context"
"fmt"

"github.com/go-logr/logr"
log "github.com/go-logr/logr"
"github.com/gravitee-io/gravitee-kubernetes-operator/api/model"
gio "github.com/gravitee-io/gravitee-kubernetes-operator/api/v1alpha1"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func Get(
ctx context.Context,
k8sClient client.Client,
log logr.Logger,
log log.Logger,
contextRef *model.ContextRef,
) (*gio.ManagementContext, error) {
apimContext := new(gio.ManagementContext)
ns := types.NamespacedName{Name: contextRef.Name, Namespace: contextRef.Namespace}

log.Info("Looking for context from", "namespace", contextRef.Namespace, "name", contextRef.Name)

err := k8sClient.Get(ctx, ns, apimContext)

if err != nil {
if err := k8sClient.Get(ctx, ns, apimContext); err != nil {
return nil, err
}

if apimContext.HasSecretRef() {
secret := new(coreV1.Secret)
secretName := apimContext.Spec.Auth.SecretRef.Name
secretNameSpace := apimContext.Spec.Auth.SecretRef.Namespace
secretKey := types.NamespacedName{Name: secretName, Namespace: secretNameSpace}

if err := k8sClient.Get(ctx, secretKey, secret); err != nil {
return nil, err
}

bearerToken, ok := secret.StringData["token"]

if !ok {
return nil, fmt.Errorf("token not found in secret %s/%s", secretNameSpace, secretName)
}

apimContext.Spec.Auth.BearerToken = bearerToken
}

return apimContext, nil
}
41 changes: 41 additions & 0 deletions docs/api/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3558,6 +3558,13 @@ ManagementContext represents the configuration for a Management API.
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#managementcontextspecauthsecretref">secretRef</a></b></td>
<td>object</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down Expand Up @@ -3593,4 +3600,38 @@ ManagementContext represents the configuration for a Management API.
</td>
<td>false</td>
</tr></tbody>
</table>


### ManagementContext.spec.auth.secretRef
<sup><sup>[↩ Parent](#managementcontextspecauth)</sup></sup>





<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b>name</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>namespace</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>
2 changes: 1 addition & 1 deletion internal/apim/managementapi/managementapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newAuthenticatedRoundTripper(
}

func (t *AuthenticatedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
t.apimCtx.Spec.Authenticate(req)
t.apimCtx.Authenticate(req)
return t.transport.RoundTrip(req)
}

Expand Down
Loading