|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2025 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package users |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "time" |
| 27 | + |
| 28 | + pbEnvoyAuthV3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" |
| 29 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 30 | + |
| 31 | + "github.com/arangodb/go-driver/v2/arangodb" |
| 32 | + "github.com/arangodb/go-driver/v2/arangodb/shared" |
| 33 | + "github.com/arangodb/go-driver/v2/connection" |
| 34 | + |
| 35 | + pbAuthenticationV1 "github.com/arangodb/kube-arangodb/integrations/authentication/v1/definition" |
| 36 | + pbImplEnvoyAuthV3Shared "github.com/arangodb/kube-arangodb/integrations/envoy/auth/v3/shared" |
| 37 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 38 | + "github.com/arangodb/kube-arangodb/pkg/util/cache" |
| 39 | + operatorHTTP "github.com/arangodb/kube-arangodb/pkg/util/http" |
| 40 | +) |
| 41 | + |
| 42 | +func New(configuration pbImplEnvoyAuthV3Shared.Configuration) (pbImplEnvoyAuthV3Shared.AuthHandler, bool) { |
| 43 | + if !configuration.Extensions.UsersCreate { |
| 44 | + return nil, false |
| 45 | + } |
| 46 | + |
| 47 | + i := &impl{ |
| 48 | + authClient: cache.NewObject[pbAuthenticationV1.AuthenticationV1Client](configuration.GetAuthClientFetcher), |
| 49 | + } |
| 50 | + |
| 51 | + i.userClient = cache.NewObject(func(ctx context.Context) (arangodb.ClientUsers, time.Duration, error) { |
| 52 | + ac, err := i.authClient.Get(ctx) |
| 53 | + if err != nil { |
| 54 | + return nil, 0, err |
| 55 | + } |
| 56 | + |
| 57 | + client := arangodb.NewClient(connection.NewHttpConnection(connection.HttpConfiguration{ |
| 58 | + Authentication: pbAuthenticationV1.NewRootRequestModifier(ac), |
| 59 | + Endpoint: connection.NewRoundRobinEndpoints([]string{ |
| 60 | + fmt.Sprintf("%s://%s:%d", configuration.Database.Proto, configuration.Database.Endpoint, configuration.Database.Port), |
| 61 | + }), |
| 62 | + ContentType: connection.ApplicationJSON, |
| 63 | + ArangoDBConfig: connection.ArangoDBConfiguration{}, |
| 64 | + Transport: operatorHTTP.RoundTripperWithShortTransport(operatorHTTP.WithTransportTLS(operatorHTTP.Insecure)), |
| 65 | + })) |
| 66 | + |
| 67 | + return client, 24 * time.Hour, nil |
| 68 | + }) |
| 69 | + |
| 70 | + i.users = cache.NewCache[string, arangodb.User](func(ctx context.Context, in string) (arangodb.User, error) { |
| 71 | + client, err := i.userClient.Get(ctx) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + if user, err := client.User(ctx, in); err == nil { |
| 77 | + return user, nil |
| 78 | + } else { |
| 79 | + if !shared.IsNotFound(err) { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if user, err := client.CreateUser(ctx, in, &arangodb.UserOptions{ |
| 85 | + Password: string(uuid.NewUUID()), |
| 86 | + Active: util.NewType(true), |
| 87 | + }); err != nil { |
| 88 | + return user, nil |
| 89 | + } else { |
| 90 | + if !shared.IsConflict(err) { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return client.User(ctx, in) |
| 96 | + }, 24*time.Hour) |
| 97 | + |
| 98 | + return i, true |
| 99 | +} |
| 100 | + |
| 101 | +type impl struct { |
| 102 | + authClient cache.Object[pbAuthenticationV1.AuthenticationV1Client] |
| 103 | + userClient cache.Object[arangodb.ClientUsers] |
| 104 | + |
| 105 | + users cache.Cache[string, arangodb.User] |
| 106 | +} |
| 107 | + |
| 108 | +func (i *impl) Handle(ctx context.Context, request *pbEnvoyAuthV3.CheckRequest, current *pbImplEnvoyAuthV3Shared.Response) error { |
| 109 | + if !current.Authenticated() { |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + _, err := i.users.Get(ctx, current.User.User) |
| 114 | + |
| 115 | + return err |
| 116 | +} |
0 commit comments