|
| 1 | +// Copyright 2021 MongoDB Inc |
| 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 mongodbatlas |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net/http" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/go-test/deep" |
| 23 | +) |
| 24 | + |
| 25 | +func TestAccessTracking_ListByCluster(t *testing.T) { |
| 26 | + client, mux, teardown := setup() |
| 27 | + defer teardown() |
| 28 | + |
| 29 | + mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/dbAccessHistory/clusters/%s", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { |
| 30 | + testMethod(t, r, http.MethodGet) |
| 31 | + fmt.Fprint(w, `{ |
| 32 | + "accessLogs": [ |
| 33 | + { |
| 34 | + "authResult": true, |
| 35 | + "authSource": "admin", |
| 36 | + "groupId": "1", |
| 37 | + "hostname": "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 38 | + "clusterName": "globalCluster", |
| 39 | + "ipAddress": "123.45.0.1", |
| 40 | + "logLine": "2019-07-25T19:14:42.484+0000 I ACCESS [conn2167] Successfully authenticated as principal jon-snow on admin from client 123.45.0.1:8080", |
| 41 | + "timestamp": "Sun Jul 25 9:14:42 EDT 2019", |
| 42 | + "username": "jon-snow" |
| 43 | + }, |
| 44 | + { |
| 45 | + "authResult": true, |
| 46 | + "authSource": "admin", |
| 47 | + "failureReason": "UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 48 | + "groupId": "1", |
| 49 | + "hostname": "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 50 | + "clusterName": "globalCluster", |
| 51 | + "ipAddress": "123.45.2.2", |
| 52 | + "logLine": "2019-07-25T19:13:39.316+0000 I ACCESS [conn1893] SASL SCRAM-SHA-1 authentication failed for jane-doe on admin from client 123.45.2.2:51842 ; UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 53 | + "timestamp": "Sun Jul 25 9:14:42 EDT 2019", |
| 54 | + "username": "jane-doe" |
| 55 | + }] |
| 56 | + }`) |
| 57 | + }) |
| 58 | + authResult := true |
| 59 | + opts := &AccessLogOptions{ |
| 60 | + Start: "1564064082000", |
| 61 | + End: "1564064082000", |
| 62 | + NLogs: 1, |
| 63 | + IPAddress: "123.45.2.2", |
| 64 | + AuthResult: &authResult, |
| 65 | + } |
| 66 | + |
| 67 | + results, _, err := client.AccessTracking.ListByCluster(ctx, groupID, clusterName, opts) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("AccessTracking.ListByCluster returned error: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + expected := &AccessLogSettings{ |
| 73 | + AccessLogs: []*AccessLogs{ |
| 74 | + { |
| 75 | + GroupID: "1", |
| 76 | + Hostname: "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 77 | + ClusterName: "globalCluster", |
| 78 | + IPAddress: "123.45.0.1", |
| 79 | + LogLine: "2019-07-25T19:14:42.484+0000 I ACCESS [conn2167] Successfully authenticated as principal jon-snow on admin from client 123.45.0.1:8080", |
| 80 | + Timestamp: "Sun Jul 25 9:14:42 EDT 2019", |
| 81 | + Username: "jon-snow", |
| 82 | + AuthSource: "admin", |
| 83 | + AuthResult: &authResult, |
| 84 | + }, |
| 85 | + { |
| 86 | + GroupID: "1", |
| 87 | + Hostname: "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 88 | + ClusterName: "globalCluster", |
| 89 | + IPAddress: "123.45.2.2", |
| 90 | + LogLine: "2019-07-25T19:13:39.316+0000 I ACCESS [conn1893] SASL SCRAM-SHA-1 authentication failed for jane-doe on admin from client 123.45.2.2:51842 ; UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 91 | + Timestamp: "Sun Jul 25 9:14:42 EDT 2019", |
| 92 | + Username: "jane-doe", |
| 93 | + AuthSource: "admin", |
| 94 | + AuthResult: &authResult, |
| 95 | + FailureReason: "UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + if diff := deep.Equal(results, expected); diff != nil { |
| 101 | + t.Error(diff) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestAccessTracking_ListByHostname(t *testing.T) { |
| 106 | + client, mux, teardown := setup() |
| 107 | + defer teardown() |
| 108 | + |
| 109 | + hostname := "cluster0-shard-00-00-c01ab.mongodb.net:27017" |
| 110 | + mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/dbAccessHistory/processes/%s", groupID, hostname), func(w http.ResponseWriter, r *http.Request) { |
| 111 | + testMethod(t, r, http.MethodGet) |
| 112 | + fmt.Fprint(w, `{ |
| 113 | + "accessLogs": [ |
| 114 | + { |
| 115 | + "authResult": true, |
| 116 | + "authSource": "admin", |
| 117 | + "groupId": "1", |
| 118 | + "hostname": "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 119 | + "clusterName": "globalCluster", |
| 120 | + "ipAddress": "123.45.0.1", |
| 121 | + "logLine": "2019-07-25T19:14:42.484+0000 I ACCESS [conn2167] Successfully authenticated as principal jon-snow on admin from client 123.45.0.1:8080", |
| 122 | + "timestamp": "Sun Jul 25 9:14:42 EDT 2019", |
| 123 | + "username": "jon-snow" |
| 124 | + }, |
| 125 | + { |
| 126 | + "authResult": true, |
| 127 | + "authSource": "admin", |
| 128 | + "failureReason": "UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 129 | + "groupId": "1", |
| 130 | + "hostname": "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 131 | + "clusterName": "globalCluster", |
| 132 | + "ipAddress": "123.45.2.2", |
| 133 | + "logLine": "2019-07-25T19:13:39.316+0000 I ACCESS [conn1893] SASL SCRAM-SHA-1 authentication failed for jane-doe on admin from client 123.45.2.2:51842 ; UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 134 | + "timestamp": "Sun Jul 25 9:14:42 EDT 2019", |
| 135 | + "username": "jane-doe" |
| 136 | + }] |
| 137 | + }`) |
| 138 | + }) |
| 139 | + |
| 140 | + authResult := true |
| 141 | + opts := &AccessLogOptions{ |
| 142 | + Start: "1564064082000", |
| 143 | + End: "1564064082000", |
| 144 | + NLogs: 1, |
| 145 | + IPAddress: "123.45.2.2", |
| 146 | + AuthResult: &authResult, |
| 147 | + } |
| 148 | + |
| 149 | + results, _, err := client.AccessTracking.ListByHostname(ctx, groupID, hostname, opts) |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("AccessTracking.ListByHostname returned error: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + expected := &AccessLogSettings{ |
| 155 | + AccessLogs: []*AccessLogs{ |
| 156 | + { |
| 157 | + GroupID: "1", |
| 158 | + Hostname: "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 159 | + ClusterName: "globalCluster", |
| 160 | + IPAddress: "123.45.0.1", |
| 161 | + LogLine: "2019-07-25T19:14:42.484+0000 I ACCESS [conn2167] Successfully authenticated as principal jon-snow on admin from client 123.45.0.1:8080", |
| 162 | + Timestamp: "Sun Jul 25 9:14:42 EDT 2019", |
| 163 | + Username: "jon-snow", |
| 164 | + AuthSource: "admin", |
| 165 | + AuthResult: &authResult, |
| 166 | + }, |
| 167 | + { |
| 168 | + GroupID: "1", |
| 169 | + Hostname: "cluster0-shard-00-00-c01ab.mongodb.net:27017", |
| 170 | + ClusterName: "globalCluster", |
| 171 | + IPAddress: "123.45.2.2", |
| 172 | + LogLine: "2019-07-25T19:13:39.316+0000 I ACCESS [conn1893] SASL SCRAM-SHA-1 authentication failed for jane-doe on admin from client 123.45.2.2:51842 ; UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 173 | + Timestamp: "Sun Jul 25 9:14:42 EDT 2019", |
| 174 | + Username: "jane-doe", |
| 175 | + AuthSource: "admin", |
| 176 | + AuthResult: &authResult, |
| 177 | + FailureReason: "UserNotFound: Could not find user \"jane-doe\" for db \"admin\"", |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + if diff := deep.Equal(results, expected); diff != nil { |
| 183 | + t.Error(diff) |
| 184 | + } |
| 185 | +} |
0 commit comments