forked from raystack/guardian
-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.go
185 lines (157 loc) · 4.73 KB
/
model.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
package bigquery
import (
"fmt"
"strings"
bq "cloud.google.com/go/bigquery"
"github.com/goto/guardian/core/resource"
"github.com/goto/guardian/domain"
"github.com/goto/guardian/utils"
"github.com/mitchellh/mapstructure"
)
const (
// ResourceTypeDataset is the resource type name for BigQuery dataset
ResourceTypeDataset = "dataset"
// ResourceTypeTable is the resource type name for BigQuery table
ResourceTypeTable = "table"
)
// Dataset is a reference to a BigQuery dataset
type Dataset struct {
ProjectID string
DatasetID string
Labels map[string]string
}
func (d *Dataset) FromDomain(r *domain.Resource) error {
if r.Type != ResourceTypeDataset {
return ErrInvalidResourceType
}
d.ProjectID = strings.TrimSuffix(r.URN, fmt.Sprintf(":%s", r.Name))
d.DatasetID = r.Name
return nil
}
func (d *Dataset) ToDomain() *domain.Resource {
r := &domain.Resource{
Type: ResourceTypeDataset,
Name: d.DatasetID,
URN: fmt.Sprintf("%s:%s", d.ProjectID, d.DatasetID),
// urn:{source}:{scope}:{kind}:{identifier}
GlobalURN: utils.GetGlobalURN("bigquery", d.ProjectID, ResourceTypeDataset, fmt.Sprintf("%s:%s", d.ProjectID, d.DatasetID)),
}
if d.Labels != nil {
if r.Details == nil {
r.Details = make(map[string]interface{})
}
r.Details[resource.ReservedDetailsKeyMetadata] = map[string]interface{}{
"labels": d.Labels,
}
}
return r
}
// Table is a reference to a BigQuery table
type Table struct {
ProjectID string
DatasetID string
TableID string
Labels map[string]string
}
func (t *Table) FromDomain(r *domain.Resource) error {
if r.Type != ResourceTypeTable {
return ErrInvalidResourceType
}
datasetURN := strings.Split(strings.TrimSuffix(r.URN, fmt.Sprintf(".%s", r.Name)), ":")
if len(datasetURN) != 2 {
return ErrInvalidTableURN
}
t.ProjectID = datasetURN[0]
t.DatasetID = datasetURN[1]
t.TableID = r.Name
return nil
}
func (t *Table) ToDomain() *domain.Resource {
r := &domain.Resource{
Type: ResourceTypeTable,
Name: t.TableID,
URN: fmt.Sprintf("%s:%s.%s", t.ProjectID, t.DatasetID, t.TableID),
GlobalURN: utils.GetGlobalURN("bigquery", t.ProjectID, ResourceTypeTable, fmt.Sprintf("%s:%s.%s", t.ProjectID, t.DatasetID, t.TableID)),
}
if t.Labels != nil {
if r.Details == nil {
r.Details = make(map[string]interface{})
}
r.Details[resource.ReservedDetailsKeyMetadata] = map[string]interface{}{
"labels": t.Labels,
}
}
return r
}
type datasetAccessEntry bq.AccessEntry
func (ae datasetAccessEntry) getEntityType() string {
switch bq.AccessEntry(ae).EntityType {
case bq.UserEmailEntity:
if strings.Contains(bq.AccessEntry(ae).Entity, "gserviceaccount.com") {
return AccountTypeServiceAccount
}
return AccountTypeUser
default:
return ""
}
}
// BigQueryResourceName is a string representation of bigquery resource's Relative Resource Name.
// Example: "projects/project-id/datasets/dataset_name/tables/table_name"
type BigQueryResourceName string
func (r BigQueryResourceName) ProjectID() string {
items := strings.Split(string(r), "/")
if len(items) >= 2 {
return items[1]
}
return ""
}
func (r BigQueryResourceName) DatasetID() string {
items := strings.Split(string(r), "/")
if len(items) >= 4 {
return items[3]
}
return ""
}
func (r BigQueryResourceName) TableID() string {
items := strings.Split(string(r), "/")
if len(items) >= 6 {
return items[5]
}
return ""
}
// BigQueryResourceID returns bigquery resource identifier in format of:
// For dataset type: "project-id:dataset_name"
// For table type: "project-id:dataset_name.table_name"
func (r BigQueryResourceName) BigQueryResourceID() string {
urn := fmt.Sprintf("%s:%s", r.ProjectID(), r.DatasetID())
if tableID := r.TableID(); tableID != "" {
urn = fmt.Sprintf("%s.%s", urn, tableID)
}
return urn
}
type cloudLoggingOptions struct {
LogBucket string `json:"log_bucket" yaml:"log_bucket" mapstructure:"log_bucket"`
}
type activityConfig struct {
*domain.ActivityConfig
}
func (c activityConfig) Validate() error {
validSources := []string{"default", "cloud_logging"}
if !utils.ContainsString(validSources, c.ActivityConfig.Source) {
return fmt.Errorf("invalid source: %q, allowed values %v", c.ActivityConfig.Source, validSources)
}
return nil
}
func (c activityConfig) GetCloudLoggingOptions() (*cloudLoggingOptions, error) {
if c.ActivityConfig == nil || (c.ActivityConfig.Source == "cloud_logging" && c.ActivityConfig.Options == nil) {
return &cloudLoggingOptions{}, nil
}
if c.ActivityConfig.Source != "cloud_logging" {
return nil, fmt.Errorf("invalid source: %q", c.ActivityConfig.Source)
}
result := &cloudLoggingOptions{}
if err := mapstructure.Decode(c.ActivityConfig.Options, result); err != nil {
return nil, fmt.Errorf("decoding options: %w", err)
}
return result, nil
}