|
| 1 | +// |
| 2 | +// Author:: Rubinder Singh (<[email protected]>) |
| 3 | +// Copyright:: Copyright 2024, Lacework Inc. |
| 4 | +// License:: Apache License, Version 2.0 |
| 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 | + |
| 19 | +package api_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/lacework/go-sdk/api" |
| 27 | + "github.com/lacework/go-sdk/internal/intgguid" |
| 28 | + "github.com/lacework/go-sdk/internal/lacework" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | +) |
| 31 | + |
| 32 | +func TestCloudAccountsAzureAdAlGet(t *testing.T) { |
| 33 | + var ( |
| 34 | + intgGUID = intgguid.New() |
| 35 | + apiPath = fmt.Sprintf("CloudAccounts/%s", intgGUID) |
| 36 | + fakeServer = lacework.MockServer() |
| 37 | + ) |
| 38 | + fakeServer.MockToken("TOKEN") |
| 39 | + defer fakeServer.Close() |
| 40 | + |
| 41 | + fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { |
| 42 | + assert.Equal(t, "GET", r.Method, "GetAzureAdAl() should be a GET method") |
| 43 | + fmt.Fprintf(w, generateCloudAccountResponse(azureAdAlCloudAccount(intgGUID))) |
| 44 | + }) |
| 45 | + |
| 46 | + c, err := api.NewClient("test", |
| 47 | + api.WithToken("TOKEN"), |
| 48 | + api.WithURL(fakeServer.URL()), |
| 49 | + ) |
| 50 | + assert.Nil(t, err) |
| 51 | + |
| 52 | + response, err := c.V2.CloudAccounts.GetAzureAdAl(intgGUID) |
| 53 | + assert.Nil(t, err) |
| 54 | + assert.NotNil(t, response) |
| 55 | + assert.Equal(t, intgGUID, response.Data.IntgGuid) |
| 56 | + assert.Equal(t, "azure_ad_al_integration_test", response.Data.Name) |
| 57 | + assert.True(t, response.Data.State.Ok) |
| 58 | + assert.Equal(t, "123456777", response.Data.Data.Credentials.ClientID) |
| 59 | + assert.Equal(t, "test-secret-1234", response.Data.Data.Credentials.ClientSecret) |
| 60 | + assert.Equal(t, "AzureAdAl", response.Data.Type) |
| 61 | + assert.Equal(t, "tenant-1", response.Data.Data.TenantID) |
| 62 | + assert.Equal(t, "eventHubNamespace-1", response.Data.Data.EventHubNamespace) |
| 63 | + assert.Equal(t, "eventHubName-1", response.Data.Data.EventHubName) |
| 64 | +} |
| 65 | + |
| 66 | +func TestCloudAccountsAzureAdAlUpdate(t *testing.T) { |
| 67 | + var ( |
| 68 | + intgGUID = intgguid.New() |
| 69 | + apiPath = fmt.Sprintf("CloudAccounts/%s", intgGUID) |
| 70 | + fakeServer = lacework.MockServer() |
| 71 | + intgData = api.AzureAdAlData{ |
| 72 | + TenantID: "tenant-1", |
| 73 | + Credentials: api.AzureAdAlCredentials{ |
| 74 | + ClientID: "123456777", |
| 75 | + ClientSecret: "test-secret-1234", |
| 76 | + }, |
| 77 | + EventHubNamespace: "eventHubNamespace-1", |
| 78 | + EventHubName: "eventHubName-1", |
| 79 | + } |
| 80 | + ) |
| 81 | + fakeServer.MockToken("TOKEN") |
| 82 | + defer fakeServer.Close() |
| 83 | + |
| 84 | + // Step 1 - Start Fake Server to return updated data |
| 85 | + fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { |
| 86 | + assert.Equal(t, "PATCH", r.Method, "UpdateAzureAdAl() should be a PATCH method") |
| 87 | + |
| 88 | + if assert.NotNil(t, r.Body) { |
| 89 | + body := httpBodySniffer(r) |
| 90 | + assert.Contains(t, body, intgGUID, "INTG_GUID missing") |
| 91 | + assert.Contains(t, body, "azure_ad_al_integration_test", "cloud account name is missing") |
| 92 | + assert.Contains(t, body, "AzureAdAl", "wrong cloud account type") |
| 93 | + assert.Contains(t, body, intgData.Credentials.ClientID, "wrong ClientId") |
| 94 | + assert.Contains(t, body, intgData.Credentials.ClientSecret, "wrong ClientSecret") |
| 95 | + assert.Contains(t, body, intgData.TenantID, "wrong TenantId") |
| 96 | + assert.Contains(t, body, intgData.EventHubNamespace, "wrong EventHubNamespace") |
| 97 | + assert.Contains(t, body, intgData.EventHubName, "wrong EventHubName") |
| 98 | + assert.Contains(t, body, "enabled\":1", "cloud account is not enabled") |
| 99 | + } |
| 100 | + |
| 101 | + fmt.Fprintf(w, generateCloudAccountResponse(azureAdAlCloudAccount(intgGUID))) |
| 102 | + }) |
| 103 | + |
| 104 | + c, err := api.NewClient("test", |
| 105 | + api.WithToken("TOKEN"), |
| 106 | + api.WithURL(fakeServer.URL()), |
| 107 | + ) |
| 108 | + assert.Nil(t, err) |
| 109 | + |
| 110 | + // Step 2 - Get Updated data from Fake server |
| 111 | + cloudAccount := api.NewCloudAccount("azure_ad_al_integration_test", |
| 112 | + api.AzureAdAlCloudAccount, |
| 113 | + intgData, |
| 114 | + ) |
| 115 | + |
| 116 | + cloudAccount.IntgGuid = intgGUID |
| 117 | + response, err := c.V2.CloudAccounts.UpdateAzureAdAl(cloudAccount) |
| 118 | + assert.Nil(t, err, "Cannot update integration") |
| 119 | + assert.NotNil(t, response) |
| 120 | + integration := response.Data |
| 121 | + assert.Equal(t, intgGUID, integration.IntgGuid) |
| 122 | + |
| 123 | + integrationData := integration.Data |
| 124 | + assert.Equal(t, "azure_ad_al_integration_test", cloudAccount.Name) |
| 125 | + assert.Equal(t, "AzureAdAl", cloudAccount.Type) |
| 126 | + assert.Equal(t, 1, cloudAccount.Enabled) |
| 127 | + assert.Equal(t, "tenant-1", integrationData.TenantID) |
| 128 | + assert.Equal(t, "eventHubNamespace-1", integrationData.EventHubNamespace) |
| 129 | + assert.Equal(t, "eventHubName-1", integrationData.EventHubName) |
| 130 | + assert.Equal(t, "123456777", integrationData.Credentials.ClientID) |
| 131 | + assert.Equal(t, "test-secret-1234", integrationData.Credentials.ClientSecret) |
| 132 | +} |
| 133 | + |
| 134 | +func azureAdAlCloudAccount(id string) string { |
| 135 | + return fmt.Sprintf(`{ |
| 136 | + "createdOrUpdatedBy": "[email protected]", |
| 137 | + "createdOrUpdatedTime": "2024-03-11T00:00:00.000Z", |
| 138 | + "enabled": 1, |
| 139 | + "intgGuid": %q, |
| 140 | + "isOrg": 0, |
| 141 | + "name": "azure_ad_al_integration_test", |
| 142 | + "state": { |
| 143 | + "ok": true, |
| 144 | + "lastUpdatedTime": 1710104691000, |
| 145 | + "lastSuccessfulTime": 1710104691000, |
| 146 | + "details": { |
| 147 | + "queueRx": "OK", |
| 148 | + "decodeNtfn": "OK", |
| 149 | + "logFileGet": "OK", |
| 150 | + "queueDel": "OK", |
| 151 | + "lastMsgRxTime": 1710104691000, |
| 152 | + "noData": true |
| 153 | + } |
| 154 | + }, |
| 155 | + "type": "AzureAdAl", |
| 156 | + "data": { |
| 157 | + "credentials": { |
| 158 | + "clientId": "123456777", |
| 159 | + "clientSecret": "test-secret-1234" |
| 160 | + }, |
| 161 | + "tenantId": "tenant-1", |
| 162 | + "eventHubNamespace": "eventHubNamespace-1", |
| 163 | + "eventHubName": "eventHubName-1" |
| 164 | + } |
| 165 | + }`, id) |
| 166 | +} |
0 commit comments