-
Notifications
You must be signed in to change notification settings - Fork 9
/
auth0pull.go
218 lines (179 loc) · 4.71 KB
/
auth0pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package auth0pull
import (
"context"
"encoding/json"
"net/url"
"os"
"sort"
"time"
"github.com/mozilla-services/foxsec-pipeline/contrib/common"
"cloud.google.com/go/datastore"
stackdriver "cloud.google.com/go/logging"
"github.com/ajvb/auth0/management"
log "github.com/sirupsen/logrus"
"go.mozilla.org/mozlogrus"
)
const (
LASTLOGID_KIND = "last_log_id_auth0"
LASTLOGID_KEY = "last_log_id_auth0"
LASTLOGID_NAMESPACE = "last_log_id_auth0"
LOGGER_NAME = "auth0pull"
)
var (
PROJECT_ID string
config = &common.Configuration{}
logClient *management.LogManager
datastoreClient *datastore.Client
stackdriverClient *stackdriver.Client
)
func init() {
mozlogrus.Enable("auth0pull")
PROJECT_ID = os.Getenv("GCP_PROJECT")
InitConfig()
}
func InitConfig() {
log.Info("Starting up...")
configPath := os.Getenv("CONFIG_PATH")
if configPath == "" {
log.Fatal("$CONFIG_PATH must be set.")
}
err := config.LoadFrom(configPath)
if err != nil {
log.Fatalf("Could not load config file from `%s`: %s", configPath, err)
}
m, err := management.New(config.Auth0Domain, config.Auth0ClientId, config.Auth0ClientSecret)
if err != nil {
log.Fatalf("Could not create management client: %s", err)
}
logClient = management.NewLogManager(m)
stackdriverClient, err = stackdriver.NewClient(context.Background(), PROJECT_ID)
if err != nil {
log.Fatalf("Could not create stackdriver client: %s", err)
}
datastoreClient, err = datastore.NewClient(context.Background(), PROJECT_ID)
if err != nil {
log.Fatalf("Could not create datastore client: %s", err)
}
}
func getLatestLogEventId() (*string, error) {
today := time.Now().Format("2006-01-02")
logs, err := logClient.List(func(v url.Values) {
v.Set("q", "date:"+today)
})
if err != nil {
log.Errorf("Error getting latest log: %s", err)
return nil, err
}
sort.Slice(logs, func(i, j int) bool {
return logs[i].Date.Before(*logs[j].Date)
})
lastLogId := logs[len(logs)-1].ID
for {
logs, err := logClient.List(func(v url.Values) {
v.Set("from", *lastLogId)
v.Set("take", "100")
})
if err != nil {
log.Errorf("Error getting latest log: %s", err)
return nil, err
}
if len(logs) == 0 {
break
}
sort.Slice(logs, func(i, j int) bool {
return logs[i].Date.Before(*logs[j].Date)
})
lastLogId = logs[len(logs)-1].ID
}
return lastLogId, nil
}
type lastLogId struct {
LastLogId string `json:"last_log_id"`
UpdatedAt time.Time `json:"updated_at"`
}
func loadLastLogId(ctx context.Context) (*lastLogId, error) {
var (
sf common.StateField
llid lastLogId
)
nk := datastore.NameKey(LASTLOGID_KIND, LASTLOGID_KEY, nil)
nk.Namespace = LASTLOGID_NAMESPACE
err := datastoreClient.Get(ctx, nk, &sf)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(sf.State), &llid)
if err != nil {
return nil, err
}
return &llid, nil
}
func (llid *lastLogId) save(ctx context.Context) error {
llid.UpdatedAt = time.Now()
buf, err := json.Marshal(llid)
if err != nil {
return err
}
nk := datastore.NameKey(LASTLOGID_KIND, LASTLOGID_KEY, nil)
nk.Namespace = LASTLOGID_NAMESPACE
tx, err := datastoreClient.NewTransaction(ctx)
if err != nil {
return err
}
if _, err := tx.Put(nk, &common.StateField{State: string(buf)}); err != nil {
return err
}
if _, err := tx.Commit(); err != nil {
return err
}
return nil
}
// PubSubMessage is used for the function signature of the main function (Duopull())
// represent the data sent from PubSub. The data is not actually read, this is only
// used as a mechanism for triggering the function (using Cloud Scheduler or similiar)
type PubSubMessage struct {
Data []byte `json:"data"`
}
func Auth0Pull(ctx context.Context, psmsg PubSubMessage) error {
llid, err := loadLastLogId(ctx)
if err != nil {
log.Errorf("Error loading last log id - Err: %s - Trying to get latest log event from auth0", err)
latestlid, err := getLatestLogEventId()
if err != nil {
log.Errorf("Failed to get latest log event from auth0: %s", err)
return err
}
llid = &lastLogId{LastLogId: *latestlid, UpdatedAt: time.Now()}
}
logger := stackdriverClient.Logger(LOGGER_NAME)
for {
logs, err := logClient.List(func(v url.Values) {
v.Set("from", llid.LastLogId)
v.Set("take", "100")
})
if err != nil {
log.Errorf("Error getting logs: %s", err)
break
}
if len(logs) == 0 {
break
}
sort.Slice(logs, func(i, j int) bool {
return logs[i].Date.Before(*logs[j].Date)
})
for _, log := range logs {
logger.Log(stackdriver.Entry{Payload: log})
}
log.Infof("auth0pull logged %d entries", len(logs))
llid.LastLogId = *logs[len(logs)-1].ID
}
err = logger.Flush()
if err != nil {
return err
}
err = llid.save(ctx)
if err != nil {
return err
}
return nil
}