-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (63 loc) · 1.5 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
package main
import (
"crypto"
"crypto/sha256"
"io"
"log"
"net/http"
"time"
"crypto/rand"
"cloud.google.com/go/storage"
salkmssign "github.com/salrashid123/signer/kms"
)
var (
projectId = "your-project"
bucketName = "your-bucket"
kmsKeyRing = "mycacerts"
kmsKey = "key1"
kmsKeyVersion = "1"
kmsLocationId = "us-central1"
serviceAccountEmail = "kms-svc-account@your-project.iam.gserviceaccount.com"
keyId = "ce4ceffd5f9c8b399df9bf7b5c13327dab65f180"
)
func main() {
r, err := salkmssign.NewKMSCrypto(&salkmssign.KMS{
ProjectId: projectId,
LocationId: kmsLocationId,
KeyRing: kmsKeyRing,
Key: kmsKey,
KeyVersion: kmsKeyVersion,
})
if err != nil {
log.Fatal(err)
}
bucket := bucketName
object := "foo.txt"
keyID := serviceAccountEmail
expires := time.Now().Add(time.Minute * 10)
s, err := storage.SignedURL(bucket, object, &storage.SignedURLOptions{
Scheme: storage.SigningSchemeV4,
GoogleAccessID: keyID,
SignBytes: func(b []byte) ([]byte, error) {
sum := sha256.Sum256(b)
return r.Sign(rand.Reader, sum[:], crypto.SHA256)
},
Method: "GET",
Expires: expires,
})
if err != nil {
log.Fatal(err)
}
log.Println(s)
resp, err := http.Get(s)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
log.Println("SignedURL Response :\n", string(body))
if err != nil {
log.Fatal(err)
}
// =============================================
}