This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprovider.go
187 lines (159 loc) · 5.58 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package authorino
import (
"context"
"fmt"
"reflect"
"github.com/go-logr/logr"
authorino "github.com/kuadrant/authorino/api/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
networkingv1beta1 "github.com/kuadrant/kuadrant-controller/apis/networking/v1beta1"
"github.com/kuadrant/kuadrant-controller/pkg/common"
"github.com/kuadrant/kuadrant-controller/pkg/reconcilers"
)
type Provider struct {
*reconcilers.BaseReconciler
}
// +kubebuilder:rbac:groups=authorino.kuadrant.io,resources=authconfigs,verbs=get;list;watch;create;update;patch;delete
func New(baseReconciler *reconcilers.BaseReconciler) *Provider {
utilruntime.Must(authorino.AddToScheme(baseReconciler.Scheme()))
return &Provider{BaseReconciler: baseReconciler}
}
func (a *Provider) Reconcile(ctx context.Context, apip *networkingv1beta1.APIProduct) (ctrl.Result, error) {
logger := logr.FromContext(ctx).WithName("authprovider").WithName("authorino")
logger.V(1).Info("Reconcile")
authConfig := buildAuthConfig(apip)
err := a.ReconcileAuthorinoAuthConfig(ctx, authConfig, authorinoAuthConfigBasicMutator)
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (a *Provider) ReconcileAuthorinoAuthConfig(ctx context.Context, desired *authorino.AuthConfig, mutatefn reconcilers.MutateFn) error {
return a.ReconcileResource(ctx, &authorino.AuthConfig{}, desired, mutatefn)
}
func buildAuthConfig(apip *networkingv1beta1.APIProduct) *authorino.AuthConfig {
authConfig := &authorino.AuthConfig{
ObjectMeta: metav1.ObjectMeta{
Name: apip.Name + apip.Namespace,
Namespace: common.KuadrantNamespace,
},
Spec: authorino.AuthConfigSpec{
Hosts: apip.Spec.Hosts,
Identity: nil,
Metadata: nil,
Authorization: nil,
Response: nil,
},
}
if !apip.HasSecurity() {
common.TagObjectToDelete(authConfig)
return authConfig
}
for _, securityScheme := range apip.Spec.SecurityScheme {
identity := authorino.Identity{
Name: securityScheme.Name,
Credentials: authorino.Credentials{},
OAuth2: nil,
Oidc: nil,
APIKey: nil,
KubernetesAuth: nil,
}
if securityScheme.APIKeyAuth != nil {
apikey := authorino.Identity_APIKey{
LabelSelectors: securityScheme.APIKeyAuth.CredentialSource.LabelSelectors,
}
identity.Credentials.In = authorino.Credentials_In(securityScheme.APIKeyAuth.Location)
identity.Credentials.KeySelector = securityScheme.APIKeyAuth.Name
identity.APIKey = &apikey
} else if securityScheme.OpenIDConnectAuth != nil {
identity.Oidc = &authorino.Identity_OidcConfig{
Endpoint: securityScheme.OpenIDConnectAuth.URL,
}
}
authConfig.Spec.Identity = append(authConfig.Spec.Identity, &identity)
}
// Response
if apip.AuthRateLimit() != nil && apip.HasAPIKeyAuth() {
response := &authorino.Response{
Name: "rate-limit-apikey",
Wrapper: authorino.Response_Wrapper("envoyDynamicMetadata"),
WrapperKey: "ext_auth_data",
JSON: &authorino.Response_DynamicJSON{
Properties: []authorino.JsonProperty{
{
Name: "user-id",
ValueFrom: authorino.ValueFromAuthJSON{
AuthJSON: `auth.identity.metadata.annotations.secret\.kuadrant\.io/user-id`,
},
},
},
},
}
authConfig.Spec.Response = append(authConfig.Spec.Response, response)
}
if apip.AuthRateLimit() != nil && apip.HasOIDCAuth() {
response := &authorino.Response{
Name: "rate-limit-oidc",
Wrapper: authorino.Response_Wrapper("envoyDynamicMetadata"),
WrapperKey: "ext_auth_data",
JSON: &authorino.Response_DynamicJSON{
Properties: []authorino.JsonProperty{
{
Name: "user-id",
ValueFrom: authorino.ValueFromAuthJSON{
AuthJSON: `auth.identity.sub`,
},
},
},
},
}
authConfig.Spec.Response = append(authConfig.Spec.Response, response)
}
return authConfig
}
func (a *Provider) Delete(ctx context.Context, apip *networkingv1beta1.APIProduct) (err error) {
log := a.Logger().WithValues("apiproduct", client.ObjectKeyFromObject(apip))
log.V(1).Info("Delete")
return nil
}
func (a *Provider) Status(ctx context.Context, apip *networkingv1beta1.APIProduct) (bool, error) {
log := a.Logger().WithValues("apiproduct", client.ObjectKeyFromObject(apip))
log.V(1).Info("Status")
// Right now, we just try to get all the objects that should have been created, and check their status.
// If any object is missing/not-created, Status returns false.
authConfig := buildAuthConfig(apip)
if !common.IsObjectTaggedToDelete(authConfig) {
existingAuthConfig := &authorino.AuthConfig{}
err := a.Client().Get(ctx, client.ObjectKeyFromObject(authConfig), existingAuthConfig)
if err != nil && errors.IsNotFound(err) {
return false, nil
} else if err != nil {
return false, err
}
// TODO(jmprusi): No status in authorino serviceConfig objects, yet.
//if !existingServiceConfig.Status.Ready {
// return false, nil
//}
}
return true, nil
}
func authorinoAuthConfigBasicMutator(existingObj, desiredObj client.Object) (bool, error) {
existing, ok := existingObj.(*authorino.AuthConfig)
if !ok {
return false, fmt.Errorf("%T is not a *authorino.AuthConfig", existingObj)
}
desired, ok := desiredObj.(*authorino.AuthConfig)
if !ok {
return false, fmt.Errorf("%T is not a *authorino.AuthConfig", desiredObj)
}
updated := false
if !reflect.DeepEqual(existing.Spec, desired.Spec) {
existing.Spec = desired.Spec
updated = true
}
return updated, nil
}