66// The auditlogstream command demonstrates managing enterprise audit log
77// streams for Azure Blob Storage using the go-github library.
88//
9- // Usage — create (github.com):
9+ // The GitHub API base URL is read from the GITHUB_API_URL environment
10+ // variable. When running inside a GitHub Actions workflow this is set
11+ // automatically.
1012//
11- // export GITHUB_AUTH_TOKEN=<your token>
12- // go run main.go create \
13- // -enterprise=my-enterprise \
14- // -container=my-container \
15- // -sas-url=<plain-text-sas-url>
16- //
17- // Usage — create (GitHub Enterprise Server):
13+ // Usage — create:
1814//
1915// export GITHUB_AUTH_TOKEN=<your token>
16+ // export GITHUB_API_URL=https://api.<domain>.ghe.com/ or https://domain/api/v3/
2017// go run main.go create \
21- // -base-url=https://github.example.com/api/v3/ \
2218// -enterprise=my-enterprise \
2319// -container=my-container \
2420// -sas-url=<plain-text-sas-url>
2521//
2622// Usage — delete:
2723//
2824// export GITHUB_AUTH_TOKEN=<your token>
25+ // export GITHUB_API_URL=https://api.<domain>.ghe.com/ or https://domain/api/v3/
2926// go run main.go delete \
30- // -base-url=https://github.example.com/api/v3/ \
3127// -enterprise=my-enterprise \
3228// -stream-id=42
3329package main
@@ -53,7 +49,7 @@ func encryptSecret(publicKeyB64, secret string) (string, error) {
5349 return "" , fmt .Errorf ("decoding public key: %w" , err )
5450 }
5551 if len (publicKeyBytes ) != 32 {
56- return "" , fmt .Errorf ("public key must be 32 bytes, got %d " , len (publicKeyBytes ))
52+ return "" , fmt .Errorf ("public key must be 32 bytes, got %v " , len (publicKeyBytes ))
5753 }
5854 var publicKey [32 ]byte
5955 copy (publicKey [:], publicKeyBytes )
@@ -68,7 +64,7 @@ func encryptSecret(publicKeyB64, secret string) (string, error) {
6864
6965func main () {
7066 if len (os .Args ) < 2 {
71- fmt .Fprintf (os .Stderr , "Usage: %s <create|delete> [flags]\n " , os .Args [0 ])
67+ fmt .Fprintf (os .Stderr , "Usage: %v <create|delete> [flags]\n " , os .Args [0 ])
7268 os .Exit (1 )
7369 }
7470
@@ -85,30 +81,32 @@ func main() {
8581
8682func runCreate (args []string ) {
8783 fs := flag .NewFlagSet ("create" , flag .ExitOnError )
88- baseURL := fs .String ("base-url" , "https://api.github.com/" , "GitHub API base URL. For GitHub Enterprise Server use https://HOSTNAME/api/v3/." )
89- enterprise := fs .String ("enterprise" , "" , "Name of the GitHub enterprise slug (required)." )
84+ enterprise := fs .String ("enterprise" , "" , "Enterprise slug (required)." )
9085 container := fs .String ("container" , "" , "Azure Blob Storage container name (required)." )
9186 sasURL := fs .String ("sas-url" , "" , "Plain-text Azure SAS URL to encrypt and submit (required)." )
9287 enabled := fs .Bool ("enabled" , true , "Whether the stream should be enabled immediately." )
93- fs .Parse (args )
88+ if err := fs .Parse (args ); err != nil {
89+ log .Fatalf ("Error parsing flags: %v" , err )
90+ }
9491
9592 token := requireEnv ("GITHUB_AUTH_TOKEN" )
93+ apiURL := requireEnv ("GITHUB_API_URL" )
9694 requireFlag ("enterprise" , * enterprise )
9795 requireFlag ("container" , * container )
9896 requireFlag ("sas-url" , * sasURL )
9997
10098 ctx := context .Background ()
101- client := newClient (token , * baseURL )
99+ client := newClient (token , apiURL )
102100
103101 // Step 1: Fetch the enterprise's public streaming key.
104102 streamKey , _ , err := client .Enterprise .GetAuditLogStreamKey (ctx , * enterprise )
105103 if err != nil {
106104 log .Fatalf ("Error fetching audit log stream key: %v" , err )
107105 }
108- fmt .Printf ("Retrieved stream key ID: %s \n " , streamKey .GetKeyID ())
106+ fmt .Printf ("Retrieved stream key ID: %v \n " , streamKey .GetKeyID ())
109107
110108 // Step 2: Encrypt the SAS URL using the public key (sealed box / crypto_box_seal).
111- encryptedSASURL , err := encryptSecret (streamKey .GetPublicKey (), * sasURL )
109+ encryptedSASURL , err := encryptSecret (streamKey .GetKey (), * sasURL )
112110 if err != nil {
113111 log .Fatalf ("Error encrypting SAS URL: %v" , err )
114112 }
@@ -118,47 +116,49 @@ func runCreate(args []string) {
118116 config := github .NewAzureBlobStreamConfig (* enabled , & github.AzureBlobConfig {
119117 KeyID : streamKey .KeyID ,
120118 Container : github .Ptr (* container ),
121- EncryptedSASURL : github .Ptr (encryptedSASURL ),
119+ EncryptedSasURL : github .Ptr (encryptedSASURL ),
122120 })
123121
124122 stream , _ , err := client .Enterprise .CreateAuditLogStream (ctx , * enterprise , config )
125123 if err != nil {
126124 log .Fatalf ("Error creating audit log stream: %v" , err )
127125 }
128126
129- fmt .Printf ("Successfully created audit log stream:\n " )
130- fmt .Printf (" ID: %d \n " , stream .GetID ())
131- fmt .Printf (" Type: %s \n " , stream .GetStreamType ())
127+ fmt .Println ("Successfully created audit log stream:" )
128+ fmt .Printf (" ID: %v \n " , stream .GetID ())
129+ fmt .Printf (" Type: %v \n " , stream .GetStreamType ())
132130 fmt .Printf (" Enabled: %v\n " , stream .GetEnabled ())
133131 fmt .Printf (" Created at: %v\n " , stream .GetCreatedAt ())
134132}
135133
136134func runDelete (args []string ) {
137135 fs := flag .NewFlagSet ("delete" , flag .ExitOnError )
138- baseURL := fs .String ("base-url" , "https://api.github.com/" , "GitHub API base URL. For GitHub Enterprise Server use https://HOSTNAME/api/v3/." )
139- enterprise := fs .String ("enterprise" , "" , "Name of the GitHub enterprise slug (required)." )
136+ enterprise := fs .String ("enterprise" , "" , "Enterprise slug (required)." )
140137 streamID := fs .Int64 ("stream-id" , 0 , "ID of the audit log stream to delete (required)." )
141- fs .Parse (args )
138+ if err := fs .Parse (args ); err != nil {
139+ log .Fatalf ("Error parsing flags: %v" , err )
140+ }
142141
143142 token := requireEnv ("GITHUB_AUTH_TOKEN" )
143+ apiURL := requireEnv ("GITHUB_API_URL" )
144144 requireFlag ("enterprise" , * enterprise )
145145 if * streamID == 0 {
146146 log .Fatal ("flag -stream-id is required" )
147147 }
148148
149149 ctx := context .Background ()
150- client := newClient (token , * baseURL )
150+ client := newClient (token , apiURL )
151151
152152 _ , err := client .Enterprise .DeleteAuditLogStream (ctx , * enterprise , * streamID )
153153 if err != nil {
154154 log .Fatalf ("Error deleting audit log stream: %v" , err )
155155 }
156156
157- fmt .Printf ("Successfully deleted audit log stream %d .\n " , * streamID )
157+ fmt .Printf ("Successfully deleted audit log stream %v .\n " , * streamID )
158158}
159159
160- func newClient (token , baseURL string ) * github.Client {
161- client , err := github .NewClient (nil ).WithAuthToken (token ).WithEnterpriseURLs (baseURL , baseURL )
160+ func newClient (token , apiURL string ) * github.Client {
161+ client , err := github .NewClient (nil ).WithAuthToken (token ).WithEnterpriseURLs (apiURL , apiURL )
162162 if err != nil {
163163 log .Fatalf ("Error creating GitHub client: %v" , err )
164164 }
@@ -168,13 +168,13 @@ func newClient(token, baseURL string) *github.Client {
168168func requireEnv (name string ) string {
169169 val := os .Getenv (name )
170170 if val == "" {
171- log .Fatalf ("environment variable %s is not set" , name )
171+ log .Fatalf ("environment variable %v is not set" , name )
172172 }
173173 return val
174174}
175175
176176func requireFlag (name , val string ) {
177177 if val == "" {
178- log .Fatalf ("flag -%s is required" , name )
178+ log .Fatalf ("flag -%v is required" , name )
179179 }
180180}
0 commit comments