(t *testing.T)
| 3954 | } |
| 3955 | |
| 3956 | func TestDatabaseConfigAuditAPI(t *testing.T) { |
| 3957 | if !base.IsEnterpriseEdition() { |
| 3958 | t.Skip("Audit logging is an EE-only feature") |
| 3959 | } |
| 3960 | |
| 3961 | rt := rest.NewRestTesterPersistentConfig(t) |
| 3962 | defer rt.Close() |
| 3963 | |
| 3964 | // check default audit config - verbose to read event names, etc. |
| 3965 | resp := rt.SendAdminRequest(http.MethodGet, "/db/_config/audit?verbose=true", "") |
| 3966 | rest.RequireStatus(t, resp, http.StatusOK) |
| 3967 | resp.DumpBody() |
| 3968 | var responseBody map[string]interface{} |
| 3969 | require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &responseBody)) |
| 3970 | assert.Equal(t, false, responseBody["enabled"].(bool)) |
| 3971 | // check we got the verbose output |
| 3972 | assert.NotEmpty(t, responseBody["events"].(map[string]interface{})[base.AuditIDPublicUserAuthenticated.String()].(map[string]interface{})["description"].(string), "expected verbose output (event description, etc.)") |
| 3973 | // check that global event IDs were not present |
| 3974 | assert.Nil(t, responseBody["events"].(map[string]interface{})[base.AuditIDSyncGatewayCollectInfoStart.String()], "expected global event ID to not be present") |
| 3975 | |
| 3976 | // enable auditing on the database (upsert) |
| 3977 | resp = rt.SendAdminRequest(http.MethodPost, "/db/_config/audit", `{"enabled":true}`) |
| 3978 | rest.RequireStatus(t, resp, http.StatusOK) |
| 3979 | |
| 3980 | // check audit config |
| 3981 | resp = rt.SendAdminRequest(http.MethodGet, "/db/_config/audit", "") |
| 3982 | rest.RequireStatus(t, resp, http.StatusOK) |
| 3983 | resp.DumpBody() |
| 3984 | responseBody = nil |
| 3985 | require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &responseBody)) |
| 3986 | assert.Equal(t, true, responseBody["enabled"].(bool)) |
| 3987 | eventsMap, ok := responseBody["events"].(map[string]interface{}) |
| 3988 | require.True(t, ok) |
| 3989 | assert.False(t, eventsMap[base.AuditIDISGRStatus.String()].(bool), "audit enabled event should be disabled by default") |
| 3990 | assert.True(t, eventsMap[base.AuditIDPublicUserAuthenticated.String()].(bool), "public user authenticated event should be enabled by default") |
| 3991 | |
| 3992 | // use event IDs returned from GET response to disable all of them |
| 3993 | for id := range eventsMap { |
| 3994 | eventsMap[id] = false |
| 3995 | } |
| 3996 | eventsJSON, err := json.Marshal(eventsMap) |
| 3997 | require.NoError(t, err) |
| 3998 | |
| 3999 | // CBG-4111: Try to disable events on top of the default (nil) set... either PUT or POST where *all* of the given IDs are set to false. Bug results in a no-op. |
| 4000 | // CBG-4157: Ensure ALL specified events were actually disabled. Bug results in some events remaining 'true', and sometimes panicking by going out-of-bounds in a slice. |
| 4001 | resp = rt.SendAdminRequest(http.MethodPost, "/db/_config/audit", fmt.Sprintf(`{"enabled":true,"events":%s}`, eventsJSON)) |
| 4002 | rest.RequireStatus(t, resp, http.StatusOK) |
| 4003 | // check all events were actually disabled |
| 4004 | resp = rt.SendAdminRequest(http.MethodGet, "/db/_config/audit", "") |
| 4005 | rest.RequireStatus(t, resp, http.StatusOK) |
| 4006 | resp.DumpBody() |
| 4007 | responseBody = nil |
| 4008 | require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &responseBody)) |
| 4009 | eventsMap, ok = responseBody["events"].(map[string]interface{}) |
| 4010 | require.True(t, ok) |
| 4011 | for id, val := range eventsMap { |
| 4012 | assert.False(t, val.(bool), "event %s should be disabled", id) |
| 4013 | } |
nothing calls this directly
no test coverage detected