|
| 1 | +/* |
| 2 | +Copyright 2025 The Kube Bind Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "net" |
| 23 | + "net/http" |
| 24 | + "os" |
| 25 | + "os/exec" |
| 26 | + "path/filepath" |
| 27 | + "sync" |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | + |
| 31 | + dexapi "github.com/dexidp/dex/api/v2" |
| 32 | + "github.com/stretchr/testify/require" |
| 33 | + "google.golang.org/grpc" |
| 34 | + grpcinsecure "google.golang.org/grpc/credentials/insecure" |
| 35 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 | + "k8s.io/apimachinery/pkg/util/wait" |
| 37 | +) |
| 38 | + |
| 39 | +var dexOnce sync.Once |
| 40 | + |
| 41 | +func StartDex(t testing.TB) { |
| 42 | + t.Helper() |
| 43 | + |
| 44 | + dexOnce.Do(func() { |
| 45 | + dexConfig := os.Getenv("DEX_CONFIG") |
| 46 | + if dexConfig == "" { |
| 47 | + filepath.Clean(filepath.Join(ToolsDir, "..", "dex-config-dev.yaml")) |
| 48 | + } |
| 49 | + |
| 50 | + t.Log("Starting dex with config", dexConfig) |
| 51 | + //nolint:gosec // G204: Subprocess launched with variable containing potentially dangerous commands |
| 52 | + dexCmd := exec.CommandContext( |
| 53 | + context.Background(), |
| 54 | + filepath.Join(ToolsDir, "dex"), |
| 55 | + "serve", |
| 56 | + dexConfig, |
| 57 | + ) |
| 58 | + require.NoError(t, dexCmd.Start()) |
| 59 | + // No cleanup required. If dex is started it will be pruned |
| 60 | + // automatically when the tests finish. |
| 61 | + }) |
| 62 | + |
| 63 | + t.Log("Wait for Dex to be ready") |
| 64 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://127.0.0.1:5556/dex/.well-known/openid-configuration", nil) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Eventually(t, func() bool { |
| 67 | + resp, err := http.DefaultClient.Do(req) |
| 68 | + if err != nil { |
| 69 | + return false |
| 70 | + } |
| 71 | + defer resp.Body.Close() |
| 72 | + return resp.StatusCode == http.StatusOK |
| 73 | + }, wait.ForeverTestTimeout, time.Millisecond*100) |
| 74 | + t.Log("Dex is ready") |
| 75 | +} |
| 76 | + |
| 77 | +func CreateDexClient(t testing.TB, addr net.Addr) { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + _, port, err := net.SplitHostPort(addr.String()) |
| 81 | + require.NoError(t, err) |
| 82 | + conn, err := grpc.NewClient("127.0.0.1:5557", grpc.WithTransportCredentials(grpcinsecure.NewCredentials())) |
| 83 | + require.NoError(t, err) |
| 84 | + defer conn.Close() |
| 85 | + client := dexapi.NewDexClient(conn) |
| 86 | + |
| 87 | + _, err = client.CreateClient(t.Context(), &dexapi.CreateClientReq{ |
| 88 | + Client: &dexapi.Client{ |
| 89 | + Id: "kube-bind-" + port, |
| 90 | + Secret: "ZXhhbXBsZS1hcHAtc2VjcmV0", |
| 91 | + RedirectUris: []string{fmt.Sprintf("http://%s/callback", addr)}, |
| 92 | + Public: true, |
| 93 | + Name: "kube-bind on port " + port, |
| 94 | + }, |
| 95 | + }) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + t.Cleanup(func() { |
| 99 | + ctx, cancel := context.WithDeadline(context.Background(), metav1.Now().Add(10*time.Second)) |
| 100 | + defer cancel() |
| 101 | + conn, err := grpc.NewClient("127.0.0.1:5557", grpc.WithTransportCredentials(grpcinsecure.NewCredentials())) |
| 102 | + require.NoError(t, err) |
| 103 | + _, err = dexapi.NewDexClient(conn).DeleteClient(ctx, &dexapi.DeleteClientReq{Id: "kube-bind-" + port}) |
| 104 | + require.NoError(t, err) |
| 105 | + }) |
| 106 | +} |
0 commit comments