|
| 1 | +package cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/aquasecurity/trivy/pkg/flag" |
| 13 | + "github.com/aquasecurity/trivy/pkg/types" |
| 14 | +) |
| 15 | + |
| 16 | +type mockCloudServer struct { |
| 17 | + server *httptest.Server |
| 18 | + configAvailable bool |
| 19 | +} |
| 20 | + |
| 21 | +func (m *mockCloudServer) Start() { |
| 22 | + m.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + if r.Header.Get("Authorization") != "Bearer valid-cloud-token" && r.Header.Get("Authorization") != "Bearer test-access-token" { |
| 24 | + w.WriteHeader(http.StatusUnauthorized) |
| 25 | + return |
| 26 | + } |
| 27 | + if r.URL.Path == "/api-keys/access-tokens" { |
| 28 | + w.WriteHeader(http.StatusCreated) |
| 29 | + w.Write([]byte(`{"token": "test-access-token"}`)) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + if r.URL.Path == "/configs/secrets/secret-config.yaml" { |
| 34 | + if !m.configAvailable { |
| 35 | + w.WriteHeader(http.StatusNotFound) |
| 36 | + return |
| 37 | + } |
| 38 | + if r.Header.Get("Authorization") != "Bearer test-access-token" { |
| 39 | + w.WriteHeader(http.StatusUnauthorized) |
| 40 | + return |
| 41 | + } |
| 42 | + w.WriteHeader(http.StatusOK) |
| 43 | + w.Write([]byte(`{"content": {"key": "value"}}`)) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + w.WriteHeader(http.StatusNotFound) |
| 48 | + })) |
| 49 | +} |
| 50 | + |
| 51 | +func (m *mockCloudServer) Close() { |
| 52 | + m.server.Close() |
| 53 | +} |
| 54 | + |
| 55 | +func TestUpdateOptsForCloudIntegration(t *testing.T) { |
| 56 | + mockServer := &mockCloudServer{} |
| 57 | + mockServer.Start() |
| 58 | + defer mockServer.Close() |
| 59 | + |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + opts *flag.Options |
| 63 | + configAvailable bool |
| 64 | + errorContains string |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "valid token and config to download", |
| 68 | + opts: &flag.Options{ |
| 69 | + CloudOptions: flag.CloudOptions{ |
| 70 | + CloudToken: "valid-cloud-token", |
| 71 | + ApiURL: mockServer.server.URL, |
| 72 | + TrivyServerURL: mockServer.server.URL, |
| 73 | + SecretConfig: true, |
| 74 | + }, |
| 75 | + ScanOptions: flag.ScanOptions{ |
| 76 | + Scanners: types.Scanners{types.SecretScanner}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + configAvailable: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "valid token but config not requested", |
| 83 | + opts: &flag.Options{ |
| 84 | + CloudOptions: flag.CloudOptions{ |
| 85 | + CloudToken: "valid-cloud-token", |
| 86 | + ApiURL: mockServer.server.URL, |
| 87 | + TrivyServerURL: mockServer.server.URL, |
| 88 | + SecretConfig: false, |
| 89 | + }, |
| 90 | + ScanOptions: flag.ScanOptions{ |
| 91 | + Scanners: types.Scanners{types.SecretScanner}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + configAvailable: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "valid token but config not available", |
| 98 | + opts: &flag.Options{ |
| 99 | + CloudOptions: flag.CloudOptions{ |
| 100 | + CloudToken: "valid-cloud-token", |
| 101 | + ApiURL: mockServer.server.URL, |
| 102 | + TrivyServerURL: mockServer.server.URL, |
| 103 | + SecretConfig: false, |
| 104 | + }, |
| 105 | + }, |
| 106 | + configAvailable: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "invalid token 401 status code", |
| 110 | + opts: &flag.Options{ |
| 111 | + CloudOptions: flag.CloudOptions{ |
| 112 | + CloudToken: "invalid-token", |
| 113 | + ApiURL: mockServer.server.URL, |
| 114 | + TrivyServerURL: mockServer.server.URL, |
| 115 | + SecretConfig: false, |
| 116 | + }, |
| 117 | + ScanOptions: flag.ScanOptions{ |
| 118 | + Scanners: types.Scanners{types.SecretScanner}, |
| 119 | + }, |
| 120 | + }, |
| 121 | + configAvailable: true, |
| 122 | + errorContains: "failed to get access token for Trivy Cloud", |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for _, tt := range tests { |
| 127 | + t.Run(tt.name, func(t *testing.T) { |
| 128 | + tempDir := t.TempDir() |
| 129 | + t.Setenv("XDG_DATA_HOME", tempDir) |
| 130 | + mockServer.configAvailable = tt.configAvailable |
| 131 | + |
| 132 | + err := UpdateOptsForCloudIntegration(context.Background(), tt.opts) |
| 133 | + |
| 134 | + if tt.errorContains != "" { |
| 135 | + require.ErrorContains(t, err, tt.errorContains) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + require.NoError(t, err) |
| 140 | + |
| 141 | + if tt.opts.CloudOptions.SecretConfig && tt.opts.ScanOptions.Scanners.Enabled(types.SecretScanner) { |
| 142 | + assert.NotEmpty(t, tt.opts.SecretOptions.SecretConfigPath) |
| 143 | + assert.FileExists(t, tt.opts.SecretOptions.SecretConfigPath) |
| 144 | + } else { |
| 145 | + assert.Empty(t, tt.opts.SecretOptions.SecretConfigPath) |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments