Repository navigation
feat: kubernetes crds for access controls #1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
419da20
feat(k8s): Support new Gateway api
contre95 2769725
feat(k8s): Support for GRPCRoute
contre95 3a216e9
Merge branch 'main' into feat/k8s_gateways
contre95 e1b1e72
fix(acl): only let a label provider define ACLs for domains it routes
contre95 5a685c4
Merge branch 'main' into feat/k8s_gateways
steveiliop56 b43cf76
refactor: use typed objects for kubernetes
steveiliop56 45e165f
tests: add tests for kubernetes service and extractors
steveiliop56 34456b9
chore: add missing acls service modification for domain normalization
steveiliop56 90e898a
Merge branch 'main' into feat/k8s_gateways
steveiliop56 996316b
chore: remove gateway extractors
steveiliop56 261d882
feat: init kube types
steveiliop56 a62ef39
feat: watch for both ingresses and tinyauth crds
steveiliop56 f98c844
feat: register crd in kube watcher
steveiliop56 b29e1bd
feat: load basic auth password from secret
steveiliop56 314758b
chore: add controller gen to codegen tools
steveiliop56 fb86f44
chore: disable kube tests for now
steveiliop56 fcc9125
fix: auto generate crd
steveiliop56 ac2a418
feat: support for loading config from kubeconfig
steveiliop56 52661a9
Merge branch 'main' into feat/k8s-crds
steveiliop56 805859c
tests: add tests for kube service and extractors
steveiliop56 1fe845d
Merge branch 'main' into feat/k8s-crds
steveiliop56 6203950
fix: rabbit comments
steveiliop56 520f304
Merge branch 'main' into feat/k8s-crds
steveiliop56 91be9b4
refactor: run locator for all available apps
steveiliop56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/tinyauthapp/tinyauth/internal/model" | ||
| "github.com/tinyauthapp/tinyauth/internal/utils/logger" | ||
| "github.com/tinyauthapp/tinyauth/pkg/apis/tinyauth/v1alpha1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
| ) | ||
|
|
||
| type KubernetesCRDInput struct { | ||
| Log *logger.Logger | ||
| Ctx context.Context | ||
| Client kubernetes.Interface | ||
| } | ||
|
|
||
| type KubernetesCRDExtractor struct { | ||
| log *logger.Logger | ||
| ctx context.Context | ||
| client kubernetes.Interface | ||
| } | ||
|
|
||
| func NewKubernetesCRDExtractor(i KubernetesCRDInput) *KubernetesCRDExtractor { | ||
| return &KubernetesCRDExtractor{ | ||
| log: i.Log, | ||
| ctx: i.Ctx, | ||
| client: i.Client, | ||
| } | ||
| } | ||
|
|
||
| func (k *KubernetesCRDExtractor) Extract(app *v1alpha1.Application) ExtractionResult { | ||
| meta := &ResourceMeta{ | ||
| Typ: ResourceTypeCRD, | ||
| Name: app.GetName(), | ||
| Namespace: app.GetNamespace(), | ||
| } | ||
|
|
||
| if !ensureResourceMeta(meta) { | ||
| k.log.App.Warn().Str("namespace", meta.Namespace).Str("name", meta.Name).Msg("Resource has no namespace or name, skipping") | ||
| return ExtractionResult{} | ||
| } | ||
|
|
||
| if app.Spec.Config.Domain == "" { | ||
| k.log.App.Warn().Str("name", meta.Name).Str("namespace", meta.Namespace).Msg("Application has no domain, skipping") | ||
| return ExtractionResult{ | ||
| Meta: meta, | ||
| } | ||
| } | ||
|
|
||
| if !ensureAscii(app.Spec.Config.Domain) { | ||
| k.log.App.Warn().Str("name", meta.Name).Str("namespace", meta.Namespace).Str("domain", app.Spec.Config.Domain).Msg("Domain is invalid, skipping") | ||
| return ExtractionResult{ | ||
| Meta: meta, | ||
| } | ||
| } | ||
|
|
||
| // Convert the CRD to the internal representation. | ||
| internalApp := app.Spec.ToInternalApp() | ||
| passwordRef := app.Spec.Response.BasicAuth.PasswordSecretRef | ||
| if passwordRef != nil { | ||
| secret, err := k.client.CoreV1().Secrets(meta.Namespace).Get(k.ctx, passwordRef.Name, metav1.GetOptions{}) | ||
| if err != nil { | ||
| k.log.App.Warn().Err(err).Str("namespace", meta.Namespace).Str("name", meta.Name).Str("secret", passwordRef.Name).Str("key", passwordRef.Key).Msg("Failed to read basic auth password Secret, skipping") | ||
| return ExtractionResult{Meta: meta} | ||
| } | ||
|
|
||
| password, ok := secret.Data[passwordRef.Key] | ||
| if !ok { | ||
| k.log.App.Warn().Str("namespace", meta.Namespace).Str("name", meta.Name).Str("secret", passwordRef.Name).Str("key", passwordRef.Key).Msg("Basic auth password Secret key does not exist, skipping") | ||
| return ExtractionResult{Meta: meta} | ||
| } | ||
|
|
||
| internalApp.Response.BasicAuth.Password = string(password) | ||
| } | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return ExtractionResult{ | ||
| Meta: meta, | ||
| Apps: map[string]model.App{ | ||
| meta.Name: internalApp, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/tinyauthapp/tinyauth/internal/model" | ||
| "github.com/tinyauthapp/tinyauth/pkg/apis/tinyauth/v1alpha1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| clientfake "k8s.io/client-go/kubernetes/fake" | ||
| ) | ||
|
|
||
| func TestKubernetesCRDExtractorExtract(t *testing.T) { | ||
| meta := &ResourceMeta{Typ: ResourceTypeCRD, Name: "dashboard", Namespace: "default"} | ||
| base := testApplication("dashboard", "dashboard.example.com") | ||
| base.Spec.Users.Allow = "alice" | ||
| base.Spec.OAuth.Groups = "admins" | ||
| base.Spec.IP.Allow = []string{"192.0.2.0/24"} | ||
| base.Spec.Path.Block = "/private" | ||
| base.Spec.Response.BasicAuth.Username = "viewer" | ||
| withRef := base.DeepCopy() | ||
| withRef.Spec.Response.BasicAuth.PasswordSecretRef = &corev1.SecretKeySelector{LocalObjectReference: corev1.LocalObjectReference{Name: "credentials"}, Key: "password"} | ||
| secret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "credentials", Namespace: "default"}, Data: map[string][]byte{"password": []byte("s3cret")}} | ||
| missingKey := secret.DeepCopy() | ||
| missingKey.Data = map[string][]byte{"other": []byte("s3cret")} | ||
| app := model.App{ | ||
| Config: model.AppConfig{Domain: "dashboard.example.com"}, | ||
| Users: model.AppUsers{Allow: "alice"}, | ||
| OAuth: model.AppOAuth{Groups: "admins"}, | ||
| IP: model.AppIP{Allow: []string{"192.0.2.0/24"}}, | ||
| Path: model.AppPath{Block: "/private"}, | ||
| Response: model.AppResponse{BasicAuth: model.AppBasicAuth{Username: "viewer"}}, | ||
| } | ||
| withPassword := app | ||
| withPassword.Response.BasicAuth.Password = "s3cret" | ||
| tests := []struct { | ||
| name string | ||
| resource *corev1.Secret | ||
| application func() *v1alpha1.Application | ||
| want ExtractionResult | ||
| reads int | ||
| }{ | ||
| {"valid application", nil, func() *v1alpha1.Application { return base.DeepCopy() }, ExtractionResult{Meta: meta, Apps: map[string]model.App{"dashboard": app}}, 0}, | ||
| {"password from secret", secret, func() *v1alpha1.Application { return withRef.DeepCopy() }, ExtractionResult{Meta: meta, Apps: map[string]model.App{"dashboard": withPassword}}, 1}, | ||
| {"secret not found", nil, func() *v1alpha1.Application { return withRef.DeepCopy() }, ExtractionResult{Meta: meta}, 1}, | ||
| {"secret key not found", missingKey, func() *v1alpha1.Application { return withRef.DeepCopy() }, ExtractionResult{Meta: meta}, 1}, | ||
| {"missing domain", nil, func() *v1alpha1.Application { a := base.DeepCopy(); a.Spec.Config.Domain = ""; return a }, ExtractionResult{Meta: meta}, 0}, | ||
| {"non-ascii domain", nil, func() *v1alpha1.Application { | ||
| a := base.DeepCopy() | ||
| a.Spec.Config.Domain = "dömain.example.com" | ||
| return a | ||
| }, ExtractionResult{Meta: meta}, 0}, | ||
| {"missing name", nil, func() *v1alpha1.Application { a := base.DeepCopy(); a.Name = ""; return a }, ExtractionResult{}, 0}, | ||
| {"missing namespace", nil, func() *v1alpha1.Application { a := base.DeepCopy(); a.Namespace = ""; return a }, ExtractionResult{}, 0}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| client := clientfake.NewClientset() | ||
| if tt.resource != nil { | ||
| client = clientfake.NewClientset(tt.resource) | ||
| } | ||
| extractor := NewKubernetesCRDExtractor(KubernetesCRDInput{Log: kubernetesTestLogger(), Client: client, Ctx: context.Background()}) | ||
| assert.Equal(t, tt.want, extractor.Extract(tt.application())) | ||
| assert.Len(t, client.Actions(), tt.reads) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.