|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + "context" |
| 13 | + |
| 14 | + "github.com/aws/aws-lambda-go/lambda" |
| 15 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 16 | + "github.com/aws/aws-sdk-go-v2/config" |
| 17 | + "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" |
| 18 | + "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" |
| 19 | + "github.com/aws/aws-sdk-go-v2/service/secretsmanager" |
| 20 | + "github.com/aws/aws-sdk-go-v2/service/ssm" |
| 21 | +) |
| 22 | + |
| 23 | +type Signins struct { |
| 24 | + Cursor string `json:"cursor"` |
| 25 | + Has_more bool `json:"has_more"` |
| 26 | + Items []Item `json:"items"` |
| 27 | +} |
| 28 | + |
| 29 | +type TargetUser struct { |
| 30 | + UUID string `json:"uuid"` |
| 31 | + Name string `json:"name"` |
| 32 | + Email string `json:"email"` |
| 33 | +} |
| 34 | + |
| 35 | +type Client struct { |
| 36 | + AppName string `json:"app_name"` |
| 37 | + AppVersion string `json:"app_version"` |
| 38 | + PlatformName string `json:"platform_name"` |
| 39 | + PlatformVersion string `json:"platform_version"` |
| 40 | + OSName string `json:"os_name"` |
| 41 | + OSVersion string `json:"os_version"` |
| 42 | + IPAddress string `json:"ip_address"` |
| 43 | +} |
| 44 | + |
| 45 | +type Location struct { |
| 46 | + Country string `json:"country"` |
| 47 | + Region string `json:"region"` |
| 48 | + City string `json:"city"` |
| 49 | + Latitude float64 `json:"latitude"` |
| 50 | + Longitude float64 `json:"longitude"` |
| 51 | +} |
| 52 | + |
| 53 | +type Item struct { |
| 54 | + UUID string `json:"uuid"` |
| 55 | + SessionUUID string `json:"session_uuid"` |
| 56 | + Timestamp time.Time `json:"timestamp"` |
| 57 | + Country string `json:"country"` |
| 58 | + Category string `json:"category"` |
| 59 | + Type string `json:"type"` |
| 60 | + Details string `json:"details"` |
| 61 | + TargetUser TargetUser `json:"target_user"` |
| 62 | + Client Client `json:"client"` |
| 63 | + Location Location `json:"location"` |
| 64 | +} |
| 65 | + |
| 66 | +var ( |
| 67 | + eventAPIurl string = "https://events.1password.com" |
| 68 | + region string = "us-east-1" |
| 69 | + secretName string = "op-events-api-token" |
| 70 | + parameterName string = "op-events-api-cursor" |
| 71 | + cloudwatchLogGroup string = "op-events-api-signins" |
| 72 | + cloudwatchStream string = "op-events-api-signins-stream" |
| 73 | +) |
| 74 | + |
| 75 | +func main() { |
| 76 | + lambda.Start(getSignInEvents) |
| 77 | +} |
| 78 | + |
| 79 | +// Retrieves data from the sign-in events endpoint. |
| 80 | +// Depending on your implementation you may want to create functions for each of the three endpoints, |
| 81 | +// or refactor this function to accept an endpoint and any other required properties, calling it once for each of the three endpoints. |
| 82 | +func getSignInEvents() { |
| 83 | + api_token := loadToken() |
| 84 | + start_time := time.Now().AddDate(0, 0, -1) |
| 85 | + |
| 86 | + currCursor := getCursor() |
| 87 | + |
| 88 | + var payload []byte |
| 89 | + if currCursor == "first_run" || currCursor == "" { |
| 90 | + payload = []byte(fmt.Sprintf(`{ |
| 91 | + "limit": 1000, |
| 92 | + "start_time": "%s" |
| 93 | + }`, start_time.Format(time.RFC3339))) |
| 94 | + } else { |
| 95 | + payload = []byte(fmt.Sprintf(`{ |
| 96 | + "cursor": "%s" |
| 97 | + }`, currCursor)) |
| 98 | + } |
| 99 | + |
| 100 | + client := &http.Client{} |
| 101 | + |
| 102 | + signinsRequest, _ := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/signinattempts", eventAPIurl), bytes.NewBuffer(payload)) |
| 103 | + signinsRequest.Header.Set("Content-Type", "application/json") |
| 104 | + signinsRequest.Header.Set("Authorization", "Bearer "+api_token) |
| 105 | + signinsResponse, signinsError := client.Do(signinsRequest) |
| 106 | + if signinsError != nil { |
| 107 | + panic(signinsError) |
| 108 | + } |
| 109 | + defer signinsResponse.Body.Close() |
| 110 | + signinsBody, _ := io.ReadAll(signinsResponse.Body) |
| 111 | + |
| 112 | + var results Signins |
| 113 | + json.Unmarshal(signinsBody, &results) |
| 114 | + |
| 115 | + if len(results.Items) != 0 { |
| 116 | + writeLogs(results.Items) |
| 117 | + setCursor(results.Cursor) |
| 118 | + } |
| 119 | + |
| 120 | + if results.Has_more { |
| 121 | + getSignInEvents() |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func loadConfig() aws.Config { |
| 126 | + config, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region)) |
| 127 | + if err != nil { |
| 128 | + log.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + return config |
| 132 | +} |
| 133 | + |
| 134 | +func loadToken() string { |
| 135 | + // Create Secrets Manager client |
| 136 | + svc := secretsmanager.NewFromConfig(loadConfig()) |
| 137 | + |
| 138 | + input := &secretsmanager.GetSecretValueInput{ |
| 139 | + SecretId: aws.String(secretName), |
| 140 | + VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified |
| 141 | + } |
| 142 | + |
| 143 | + result, err := svc.GetSecretValue(context.TODO(), input) |
| 144 | + if err != nil { |
| 145 | + // For a list of exceptions thrown, see |
| 146 | + // https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html |
| 147 | + log.Fatal(err.Error()) |
| 148 | + } |
| 149 | + |
| 150 | + // Decrypts secret using the associated KMS key. |
| 151 | + return *result.SecretString |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +// Get API cursor from ParameterStore. Could be refactored to take an endpoint as |
| 156 | +// an argument and fetch the corresponding cursor. |
| 157 | +func getCursor() string { |
| 158 | + paramInput := ssm.GetParameterInput{ |
| 159 | + Name: aws.String(parameterName), |
| 160 | + } |
| 161 | + paramStore := ssm.NewFromConfig(loadConfig()) |
| 162 | + output, err := paramStore.GetParameter(context.TODO(), ¶mInput) |
| 163 | + if err != nil { |
| 164 | + log.Print(err) |
| 165 | + return "" |
| 166 | + } |
| 167 | + |
| 168 | + return *output.Parameter.Value |
| 169 | +} |
| 170 | + |
| 171 | +// Writes new cursor to parameter store. Could be refactored to take an endpoint as |
| 172 | +// an argument and fetch the corresponding cursor. |
| 173 | +func setCursor(cursor string) { |
| 174 | + paramInput := ssm.PutParameterInput{ |
| 175 | + Name: aws.String(parameterName), |
| 176 | + Value: &cursor, |
| 177 | + Overwrite: aws.Bool(true), |
| 178 | + } |
| 179 | + paramStore := ssm.NewFromConfig(loadConfig()) |
| 180 | + |
| 181 | + paramStore.PutParameter(context.TODO(), ¶mInput) |
| 182 | +} |
| 183 | + |
| 184 | +// Write logs to destination. In this case CloudWatch, but would require refactoring |
| 185 | +// for your specific implementation and destination. |
| 186 | +func writeLogs(logItems []Item) { |
| 187 | + // Create a CloudWatchLogs client with additional configuration |
| 188 | + svc := cloudwatchlogs.NewFromConfig(loadConfig()) |
| 189 | + |
| 190 | + // Each array of InputLogEvent's needs to be within a 24-hour window |
| 191 | + // In theory, this is only an issue on first call |
| 192 | + var events []types.InputLogEvent |
| 193 | + for _, item := range logItems { |
| 194 | + timestamp := item.Timestamp.UnixMilli() |
| 195 | + itemByte, _ := json.Marshal(item) |
| 196 | + itemString := string(itemByte) |
| 197 | + event := types.InputLogEvent{ |
| 198 | + Message: &itemString, |
| 199 | + Timestamp: ×tamp, |
| 200 | + } |
| 201 | + |
| 202 | + events = append(events, event) |
| 203 | + } |
| 204 | + |
| 205 | + logEvent := cloudwatchlogs.PutLogEventsInput{ |
| 206 | + LogEvents: events, |
| 207 | + LogGroupName: &cloudwatchLogGroup, |
| 208 | + LogStreamName: &cloudwatchStream, |
| 209 | + } |
| 210 | + |
| 211 | + _, err := svc.PutLogEvents(context.TODO(), &logEvent) |
| 212 | + |
| 213 | + // log.Println("writeLogs error:") |
| 214 | + log.Println(err) |
| 215 | +} |
0 commit comments