-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant_bigquery_log.go
110 lines (96 loc) · 4.1 KB
/
grant_bigquery_log.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
// Copyright 2017 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package serviceaccounts
import (
"context"
"fmt"
"net"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/appengine"
"github.com/TriggerMail/luci-go/common/bq"
"github.com/TriggerMail/luci-go/tokenserver/api"
"github.com/TriggerMail/luci-go/tokenserver/api/admin/v1"
bqpb "github.com/TriggerMail/luci-go/tokenserver/api/bq"
"github.com/TriggerMail/luci-go/tokenserver/api/minter/v1"
"github.com/TriggerMail/luci-go/tokenserver/appengine/impl/utils"
"github.com/TriggerMail/luci-go/tokenserver/appengine/impl/utils/bqlog"
)
var oauthTokenGrantsLog = bqlog.Log{
QueueName: "bqlog-oauth-token-grants", // see queues.yaml
DatasetID: "tokens", // see bq/README.md
TableID: "oauth_token_grants", // see bq/tables/oauth_token_grants.schema
DumpEntriesToLogger: true,
DryRun: appengine.IsDevAppServer(),
}
// MintedGrantInfo is passed to LogGrant.
//
// It carries all information about the grant minting operation and the produced
// grant token.
type MintedGrantInfo struct {
Request *minter.MintOAuthTokenGrantRequest // RPC input, as is
Response *minter.MintOAuthTokenGrantResponse // RPC output, as is
GrantBody *tokenserver.OAuthTokenGrantBody // deserialized grant
ConfigRev string // revision of the service_accounts.cfg used
Rule *admin.ServiceAccountRule // the particular rule used to authorize the request
PeerIP net.IP // caller IP address
RequestID string // GAE request ID that handled the RPC
AuthDBRev int64 // revision of groups database (or 0 if unknown)
}
// toBigQueryMessage returns a message to upload to BigQuery.
func (i *MintedGrantInfo) toBigQueryMessage() *bqpb.OAuthTokenGrant {
return &bqpb.OAuthTokenGrant{
// Information about the produced token.
Fingerprint: utils.TokenFingerprint(i.Response.GrantToken),
TokenId: fmt.Sprintf("%d", i.GrantBody.TokenId),
ServiceAccount: i.GrantBody.ServiceAccount,
ProxyIdentity: i.GrantBody.Proxy,
EndUserIdentity: i.GrantBody.EndUser,
IssuedAt: i.GrantBody.IssuedAt,
Expiration: ×tamp.Timestamp{
Seconds: i.GrantBody.IssuedAt.Seconds + i.GrantBody.ValidityDuration,
Nanos: i.GrantBody.IssuedAt.Nanos,
},
// Information supplied by the caller.
AuditTags: i.Request.AuditTags,
// Information about the service account rule used.
ConfigRev: i.ConfigRev,
ConfigRule: i.Rule.Name,
// Information about the request handler environment.
PeerIp: i.PeerIP.String(),
ServiceVersion: i.Response.ServiceVersion,
GaeRequestId: i.RequestID,
AuthDbRev: i.AuthDBRev,
}
}
// LogGrant records information about the OAuth token grant in the BigQuery.
//
// The grant itself is not logged. Only first 16 bytes of its SHA256 hash
// (aka 'fingerprint') is. It is used only to identify this particular token in
// logs.
//
// On dev server, logs to the GAE log only, not to BigQuery (to avoid
// accidentally pushing fake data to real BigQuery dataset).
func LogGrant(c context.Context, i *MintedGrantInfo) error {
return oauthTokenGrantsLog.Insert(c, &bq.Row{
Message: i.toBigQueryMessage(),
})
}
// FlushGrantsLog sends all buffered logged grants to BigQuery.
//
// It is fine to call FlushGrantLog concurrently from multiple request handlers,
// if necessary (it will effectively parallelize the flush).
func FlushGrantsLog(c context.Context) error {
_, err := oauthTokenGrantsLog.Flush(c)
return err
}