/
table_gcp_apikeys_key.go
208 lines (183 loc) · 5.86 KB
/
table_gcp_apikeys_key.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
package gcp
import (
"context"
"github.com/turbot/go-kit/types"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
"google.golang.org/api/apikeys/v2"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
//// TABLE DEFINITION
func tableGcpApiKeysKey(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_apikeys_key",
Description: "GCP API Keys Key",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getApiKeysKey,
},
List: &plugin.ListConfig{
Hydrate: listApiKeysKeys,
},
Columns: []*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name").Transform(lastPathElement),
Description: "The resource name of the key.",
},
{
Name: "uid",
Type: proto.ColumnType_STRING,
Description: "Unique id in UUID4 format.",
},
{
Name: "create_time",
Description: "A timestamp identifying the time this key was originally created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "delete_time",
Description: "A timestamp when this key was deleted.",
Transform: transform.FromField("DeleteTime").Transform(transform.NullIfZeroValue),
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "display_name",
Type: proto.ColumnType_STRING,
Description: "Human-readable display name of this key that you can modify.",
},
{
Name: "etag",
Description: "A checksum computed by the server based on the current value of the Key resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "update_time",
Description: "A timestamp identifying the time this key was last updated.",
Type: proto.ColumnType_TIMESTAMP,
},
// JSON columns
{
Name: "annotations",
Description: "Annotations is an unstructured key-value map stored with a policy that may be set by external tools to store and retrieve arbitrary metadata.",
Type: proto.ColumnType_JSON,
},
{
Name: "restrictions",
Description: "The restrictions on the key.",
Type: proto.ColumnType_JSON,
},
// standard steampipe columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DisplayName"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromP(gcpApiKeyTurbotData, "Akas"),
},
// standard gcp columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromConstant("global"),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Hydrate: plugin.HydrateFunc(getProject).WithCache(),
Transform: transform.FromValue(),
},
},
}
}
//// FETCH FUNCTIONS
func listApiKeysKeys(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
logger.Error("gcp_api_key.listApiKeysKeys", "cache_error", err)
return nil, err
}
project := projectId.(string)
// Page size should be in range of [0, 300].
pageSize := types.Int64(300)
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < *pageSize {
pageSize = limit
}
}
// Create Service Connection
service, err := APIKeysService(ctx, d)
if err != nil {
logger.Error("gcp_api_key.listApiKeysKeys", "service_error", err)
return nil, err
}
// NOTE: Key is a global resource; hence the only supported value for location is `global`.
resp := service.Projects.Locations.Keys.List("projects/" + project + "/locations/global").PageSize(*pageSize)
if err := resp.Pages(
ctx,
func(page *apikeys.V2ListKeysResponse) error {
for _, item := range page.Keys {
d.StreamListItem(ctx, item)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
page.NextPageToken = ""
return nil
}
}
return nil
},
); err != nil {
logger.Error("gcp_api_key.listApiKeysKeys", "api_error", err)
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getApiKeysKey(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
logger.Error("gcp_api_key.getApiKeysKey", "cache_error", err)
return nil, err
}
project := projectId.(string)
name := d.EqualsQuals["name"].GetStringValue()
// Validate - name should not be blank
if name == "" {
return nil, nil
}
// Create Service Connection
service, err := APIKeysService(ctx, d)
if err != nil {
logger.Error("gcp_api_key.getApiKeysKey", "service_error", err)
return nil, err
}
// NOTE: Key is a global resource; hence the only supported value for location is `global`.
op, err := service.Projects.Locations.Keys.Get("projects/" + project + "/locations/global/keys/" + name).Do()
if err != nil {
logger.Error("gcp_api_key.getApiKeysKey", "api_error", err)
return nil, err
}
return op, nil
}
// /// TRANSFORM FUNCTIONS
func gcpApiKeyTurbotData(ctx context.Context, d *transform.TransformData) (interface{}, error) {
keyData := d.HydrateItem.(*apikeys.V2Key)
akas := []string{"gcp://iam.googleapis.com/" + keyData.Name}
return akas, nil
}