-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathmain.go
More file actions
179 lines (155 loc) · 5.44 KB
/
main.go
File metadata and controls
179 lines (155 loc) · 5.44 KB
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
// Copyright 2026 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The auditlogstream command demonstrates managing enterprise audit log
// streams for Azure Blob Storage using the go-github library.
//
// The GitHub API base URL is read from the GITHUB_API_URL environment
// variable. When running inside a GitHub Actions workflow this is set
// automatically.
//
// Usage — create:
//
// export GITHUB_AUTH_TOKEN=<your token>
// export GITHUB_API_URL=https://api.<domain>.ghe.com/ or https://domain/api/v3/
// go run main.go create \
// -enterprise=my-enterprise \
// -container=my-container \
// -sas-url=<plain-text-sas-url>
//
// Usage — delete:
//
// export GITHUB_AUTH_TOKEN=<your token>
// export GITHUB_API_URL=https://api.<domain>.ghe.com/ or https://domain/api/v3/
// go run main.go delete \
// -enterprise=my-enterprise \
// -stream-id=42
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"log"
"os"
"github.com/google/go-github/v83/github"
"golang.org/x/crypto/nacl/box"
)
// encryptSecret encrypts a plain-text secret using libsodium's sealed box
// (crypto_box_seal), which is what GitHub's API expects for encrypted credentials.
func encryptSecret(publicKeyB64, secret string) (string, error) {
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyB64)
if err != nil {
return "", fmt.Errorf("decoding public key: %w", err)
}
if len(publicKeyBytes) != 32 {
return "", fmt.Errorf("public key must be 32 bytes, got %v", len(publicKeyBytes))
}
publicKey := [32]byte(publicKeyBytes
encrypted, err := box.SealAnonymous(nil, []byte(secret), &publicKey, rand.Reader)
if err != nil {
return "", fmt.Errorf("encrypting secret: %w", err)
}
return base64.StdEncoding.EncodeToString(encrypted), nil
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %v <create|delete> [flags]\n", os.Args[0])
os.Exit(1)
}
switch os.Args[1] {
case "create":
runCreate(os.Args[2:])
case "delete":
runDelete(os.Args[2:])
default:
fmt.Fprintf(os.Stderr, "Unknown command %q. Must be one of: create, delete\n", os.Args[1])
os.Exit(1)
}
}
func runCreate(args []string) {
fs := flag.NewFlagSet("create", flag.ExitOnError)
enterprise := fs.String("enterprise", "", "Enterprise slug (required).")
container := fs.String("container", "", "Azure Blob Storage container name (required).")
sasURL := fs.String("sas-url", "", "Plain-text Azure SAS URL to encrypt and submit (required).")
enabled := fs.Bool("enabled", true, "Whether the stream should be enabled immediately.")
if err := fs.Parse(args); err != nil {
log.Fatalf("Error parsing flags: %v", err)
}
token := requireEnv("GITHUB_AUTH_TOKEN")
apiURL := requireEnv("GITHUB_API_URL")
requireFlag("enterprise", *enterprise)
requireFlag("container", *container)
requireFlag("sas-url", *sasURL)
ctx := context.Background()
client := newClient(token, apiURL)
// Step 1: Fetch the enterprise's public streaming key.
streamKey, _, err := client.Enterprise.GetAuditLogStreamKey(ctx, *enterprise)
if err != nil {
log.Fatalf("Error fetching audit log stream key: %v", err)
}
fmt.Printf("Retrieved stream key ID: %v\n", streamKey.GetKeyID())
// Step 2: Encrypt the SAS URL using the public key (sealed box / crypto_box_seal).
encryptedSASURL, err := encryptSecret(streamKey.GetKey(), *sasURL)
if err != nil {
log.Fatalf("Error encrypting SAS URL: %v", err)
}
fmt.Println("SAS URL encrypted successfully.")
// Step 3: Create the audit log stream.
config := github.NewAzureBlobStreamConfig(*enabled, &github.AzureBlobConfig{
KeyID: streamKey.KeyID,
Container: container,
EncryptedSasURL: &encryptedSASURL,
})
stream, _, err := client.Enterprise.CreateAuditLogStream(ctx, *enterprise, config)
if err != nil {
log.Fatalf("Error creating audit log stream: %v", err)
}
fmt.Println("Successfully created audit log stream:")
fmt.Printf(" ID: %v\n", stream.GetID())
fmt.Printf(" Type: %v\n", stream.GetStreamType())
fmt.Printf(" Enabled: %v\n", stream.GetEnabled())
fmt.Printf(" Created at: %v\n", stream.GetCreatedAt())
}
func runDelete(args []string) {
fs := flag.NewFlagSet("delete", flag.ExitOnError)
enterprise := fs.String("enterprise", "", "Enterprise slug (required).")
streamID := fs.Int64("stream-id", 0, "ID of the audit log stream to delete (required).")
if err := fs.Parse(args); err != nil {
log.Fatalf("Error parsing flags: %v", err)
}
token := requireEnv("GITHUB_AUTH_TOKEN")
apiURL := requireEnv("GITHUB_API_URL")
requireFlag("enterprise", *enterprise)
if *streamID == 0 {
log.Fatal("flag -stream-id is required")
}
ctx := context.Background()
client := newClient(token, apiURL)
_, err := client.Enterprise.DeleteAuditLogStream(ctx, *enterprise, *streamID)
if err != nil {
log.Fatalf("Error deleting audit log stream: %v", err)
}
fmt.Printf("Successfully deleted audit log stream %v.\n", *streamID)
}
func newClient(token, apiURL string) *github.Client {
client, err := github.NewClient(nil).WithAuthToken(token).WithEnterpriseURLs(apiURL, apiURL)
if err != nil {
log.Fatalf("Error creating GitHub client: %v", err)
}
return client
}
func requireEnv(name string) string {
val := os.Getenv(name)
if val == "" {
log.Fatalf("environment variable %v is not set", name)
}
return val
}
func requireFlag(name, val string) {
if val == "" {
log.Fatalf("flag -%v is required", name)
}
}