-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
149 lines (134 loc) · 4.05 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
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/cli/browser"
"github.com/int128/oauth2cli"
"github.com/int128/oauth2cli/oauth2params"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
)
//go:embed cert/server.crt
var cert []byte
//go:embed cert/server.key
var key []byte
type cmdOptions struct {
clientID string
clientSecret string
scopes string
region string
localServerCert string
localServerKey string
port int
}
func main() {
log.SetOutput(os.Stderr)
var o cmdOptions
flag.StringVar(&o.clientID, "client-id", "", "OAuth Client ID")
flag.StringVar(&o.clientSecret, "client-secret", "", "OAuth Client Secret")
flag.StringVar(&o.scopes, "scopes", "all", "Scopes to request, comma separated")
flag.StringVar(&o.region, "region", "eu", "Region to use")
flag.StringVar(&o.localServerCert, "server-crt", "", "Path to a certificate file for the local server")
flag.StringVar(&o.localServerKey, "server-key", "", "Path to a key file for the local server")
flag.IntVar(&o.port, "port", 8080, "Port for the local server")
flag.Parse()
if o.clientID == "" || o.clientSecret == "" {
log.Printf(`You need to set oauth2 credentials.
Create a service account at https://eu.api.ovh.com/console/?section=%%2Fme&branch=v1#post-/me/api/oauth2/client with AUTHORIZATION_CODE flow
Then set the following options:`)
flag.PrintDefaults()
os.Exit(1)
return
}
if o.localServerCert == "" || o.localServerKey == "" {
cleanup := setupEmbeddedCerts(&o)
defer cleanup()
}
regionEndpoints := map[string]string{
"eu": "https://www.ovh.com",
"ca": "https://ca.ovh.com",
}
endpoint, ok := regionEndpoints[o.region]
if !ok {
log.Fatalf("region %s is not supported", o.region)
}
pkce, err := oauth2params.NewPKCE()
if err != nil {
log.Fatalf("error: %s", err)
}
ready := make(chan string, 1)
defer close(ready)
cfg := oauth2cli.Config{
OAuth2Config: oauth2.Config{
ClientID: o.clientID,
ClientSecret: o.clientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/auth/oauth2/authorize", endpoint),
TokenURL: fmt.Sprintf("%s/auth/oauth2/token", endpoint),
},
Scopes: []string{"all"},
},
AuthCodeOptions: pkce.AuthCodeOptions(),
TokenRequestOptions: pkce.TokenRequestOptions(),
LocalServerReadyChan: ready,
LocalServerCertFile: o.localServerCert,
LocalServerKeyFile: o.localServerKey,
LocalServerBindAddress: []string{fmt.Sprintf("localhost:%d", o.port)},
Logf: log.Printf,
}
ctx := context.Background()
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
select {
case url := <-ready:
if err := browser.OpenURL(url); err != nil {
log.Printf("could not open the browser: %s", err)
}
return nil
case <-ctx.Done():
return fmt.Errorf("context done while waiting for authorization: %w", ctx.Err())
}
})
eg.Go(func() error {
token, err := oauth2cli.GetToken(ctx, cfg)
if err != nil {
return fmt.Errorf("could not get a token: %w", err)
}
setEnvVar(o.region, token.AccessToken)
return nil
})
if err := eg.Wait(); err != nil {
log.Fatalf("authorization error: %s", err)
}
}
func setEnvVar(region string, accessToken string) {
confFile := fmt.Sprintf(`[default]
endpoint=ovh-%s
[ovh-%s]
access_token=%s`, region, region, accessToken)
os.WriteFile("ovh.conf", []byte(confFile), 0644)
}
func setupEmbeddedCerts(o *cmdOptions) (cleanup func()) {
tmpDir := os.TempDir()
o.localServerCert = filepath.Join(tmpDir, "server.crt")
o.localServerKey = filepath.Join(tmpDir, "server.key")
if err := os.WriteFile(o.localServerCert, cert, 0644); err != nil {
log.Fatalf("could not write certificate file: %s", err)
}
if err := os.WriteFile(o.localServerKey, key, 0644); err != nil {
log.Fatalf("could not write key file: %s", err)
}
return func() {
if err := os.Remove(o.localServerCert); err != nil {
log.Printf("could not remove certificate file: %s", err)
}
if err := os.Remove(o.localServerKey); err != nil {
log.Printf("could not remove key file: %s", err)
}
}
}