istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/authz/server.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authz 16 17 import ( 18 "istio.io/istio/pkg/test/framework" 19 "istio.io/istio/pkg/test/framework/components/namespace" 20 "istio.io/istio/pkg/test/framework/resource" 21 ) 22 23 // Server for custom authz. 24 type Server interface { 25 Namespace() namespace.Instance 26 27 // Providers returns the list of Provider instances. 28 Providers() []Provider 29 } 30 31 // New creates a new authz Server. 32 func New(ctx resource.Context, ns namespace.Instance) (Server, error) { 33 return newKubeServer(ctx, ns) 34 } 35 36 // NewOrFail calls New and fails if an error occurs. 37 func NewOrFail(t framework.TestContext, ns namespace.Instance) Server { 38 t.Helper() 39 s, err := New(t, ns) 40 if err != nil { 41 t.Fatal(err) 42 } 43 return s 44 } 45 46 // NewLocal does not deploy a new server, but instead configures Istio 47 // to allow calls to a local authz server running as a sidecar to the echo 48 // app. 49 func NewLocal(ctx resource.Context, ns namespace.Instance) (Server, error) { 50 return newLocalKubeServer(ctx, ns) 51 } 52 53 // NewLocalOrFail calls NewLocal and fails if an error occurs. 54 func NewLocalOrFail(t framework.TestContext, ns namespace.Instance) Server { 55 t.Helper() 56 s, err := NewLocal(t, ns) 57 if err != nil { 58 t.Fatal(err) 59 } 60 return s 61 } 62 63 // Setup is a utility function for configuring a global authz Server. 64 func Setup(server *Server, ns namespace.Getter) resource.SetupFn { 65 if ns == nil { 66 ns = namespace.NilGetter 67 } 68 69 return func(ctx resource.Context) error { 70 s, err := New(ctx, ns()) 71 if err != nil { 72 return err 73 } 74 75 // Store the server. 76 *server = s 77 return err 78 } 79 } 80 81 // SetupLocal is a utility function for setting a global variable for a local Server. 82 func SetupLocal(server *Server, ns namespace.Getter) resource.SetupFn { 83 if ns == nil { 84 ns = namespace.NilGetter 85 } 86 87 return func(ctx resource.Context) error { 88 s, err := NewLocal(ctx, ns()) 89 if err != nil { 90 return err 91 } 92 93 // Store the server. 94 *server = s 95 return err 96 } 97 }