/
main.go
219 lines (183 loc) · 6.08 KB
/
main.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
219
/*
* Copyright 2017 Google Inc.
*
* 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 main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/cloudfoundry/bosh-gcscli/client"
"github.com/cloudfoundry/bosh-gcscli/config"
"golang.org/x/net/context"
)
var version = "dev"
// usageExample provides examples of how to use the CLI.
const usageExample = `
# Usage
bosh-gcscli --help
# Upload a blob to the GCS blobstore.
bosh-gcscli -c config.json put <path/to/file> <remote-blob>
# Fetch a blob from the GCS blobstore.
# Destination file will be overwritten if exists.
bosh-gcscli -c config.json get <remote-blob> <path/to/file>
# Remove a blob from the GCS blobstore.
bosh-gcscli -c config.json delete <remote-blob>
# Checks if blob exists in the GCS blobstore.
bosh-gcscli -c config.json exists <remote-blob>
# Generate a signed url for an object
# if an encryption key is present in config, the appropriate header will be sent
# users of the signed url must include encryption headers in request
# Where:
# - <http action> is GET, PUT, or DELETE
# - <expiry> is a duration string less than 7 days (e.g. "6h")
# eg bosh-gcscli -c config.json sign blobid PUT 24h
bosh-gcscli -c config.json sign <remote-blob> <http action> <expiry>`
var (
showVer = flag.Bool("v", false, "Print CLI version")
shortHelp = flag.Bool("h", false, "Print this help text")
longHelp = flag.Bool("help", false, "Print this help text")
configPath = flag.String("c", "",
`path to a JSON file with the following contents:
{
"bucket_name": "name of Google Cloud Storage bucket (required)",
"credentials_source": "Optional, defaults to Application Default Credentials or none)
(can be 'static' for a service account specified in json_key),
(can be 'none' for explicitly no credentials)"
"json_key": "JSON Service Account File
(optional, required for 'static' credentials)",
"storage_class": "storage class for objects
(optional, defaults to bucket settings)",
"encryption_key": "Base64 encoded 32 byte Customer-Supplied
encryption key used to encrypt objects
(optional, defaults to GCS controlled key)"
}
storage_class is one of MULTI_REGIONAL, REGIONAL, NEARLINE, or COLDLINE.
For more information on characteristics and location compatibility:
https://cloud.google.com/storage/docs/storage-classes
For more information on Customer-Supplied encryption keys:
https://cloud.google.com/storage/docs/encryption
`)
)
func main() {
flag.Parse()
if *showVer {
fmt.Printf("version %s\n", version)
os.Exit(0)
}
if *shortHelp || *longHelp || len(flag.Args()) == 0 {
flag.Usage()
fmt.Println(usageExample)
os.Exit(0)
}
if *configPath == "" {
log.Fatalf("no config file provided\nSee -help for usage\n")
}
configFile, err := os.Open(*configPath)
if err != nil {
log.Fatalf("opening config %s: %v\n", *configPath, err)
}
gcsConfig, err := config.NewFromReader(configFile)
if err != nil {
log.Fatalf("reading config %s: %v\n", *configPath, err)
}
ctx := context.Background()
blobstoreClient, err := client.New(ctx, &gcsConfig)
if err != nil {
log.Fatalf("creating gcs client: %v\n", err)
}
nonFlagArgs := flag.Args()
if len(nonFlagArgs) < 2 {
log.Fatalf("Expected at least two arguments got %d\n", len(nonFlagArgs))
}
cmd := nonFlagArgs[0]
switch cmd {
case "put":
if len(nonFlagArgs) != 3 {
log.Fatalf("put method expected 2 arguments got %d\n", len(nonFlagArgs))
}
src, dst := nonFlagArgs[1], nonFlagArgs[2]
var sourceFile *os.File
sourceFile, err = os.Open(src)
if err != nil {
log.Fatalln(err)
}
defer sourceFile.Close()
err = blobstoreClient.Put(sourceFile, dst)
fmt.Println(err)
case "get":
if len(nonFlagArgs) != 3 {
log.Fatalf("get method expected 2 arguments got %d\n", len(nonFlagArgs))
}
src, dst := nonFlagArgs[1], nonFlagArgs[2]
var dstFile *os.File
dstFile, err = os.Create(dst)
if err != nil {
log.Fatalln(err)
}
defer dstFile.Close()
err = blobstoreClient.Get(src, dstFile)
case "delete":
if len(nonFlagArgs) != 2 {
log.Fatalf("delete method expected 2 arguments got %d\n", len(nonFlagArgs))
}
err = blobstoreClient.Delete(nonFlagArgs[1])
case "exists":
if len(nonFlagArgs) != 2 {
log.Fatalf("exists method expected 2 arguments got %d\n", len(nonFlagArgs))
}
var exists bool
exists, err = blobstoreClient.Exists(nonFlagArgs[1])
// If the object exists the exit status is 0, otherwise it is 3
// We are using `3` since `1` and `2` have special meanings
if err == nil && !exists {
os.Exit(3)
}
case "sign":
if len(nonFlagArgs) != 4 {
log.Fatalf("sign method expected 3 arguments got %d\n", len(nonFlagArgs))
}
id, action, expiry := nonFlagArgs[1], nonFlagArgs[2], nonFlagArgs[3]
action = strings.ToUpper(action)
err = validateAction(action)
if err != nil {
log.Fatal(err)
}
var expiryDuration time.Duration
expiryDuration, err = time.ParseDuration(expiry)
if err != nil {
log.Fatalf("Invalid expiry duration: %v", err)
}
url := ""
url, err = blobstoreClient.Sign(id, action, expiryDuration)
if err == nil {
os.Stdout.WriteString(url)
}
default:
log.Fatalf("unknown command: '%s'\n", cmd)
}
if err != nil {
log.Fatalf("performing operation %s: %s\n", cmd, err)
}
}
func validateAction(action string) error {
if action != http.MethodGet && action != http.MethodPut && action != http.MethodDelete {
return fmt.Errorf("invalid signing action: %s must be GET, PUT, or DELETE", action)
}
return nil
}