-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
223 lines (205 loc) · 5.26 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
220
221
222
223
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/Azure/go-autorest/autorest/azure/cli"
"github.com/moriyoshi/az-cloud-shell-access/internal/oauth2"
"github.com/shibukawa/configdir"
"github.com/spf13/cobra"
)
var progName = filepath.Base(os.Args[0])
func message(msg string) {
fmt.Fprintf(os.Stderr, "%s: %s\n", progName, msg)
}
type ErrNoDefaultSubscriptionFound struct{}
func (*ErrNoDefaultSubscriptionFound) Error() string {
return "no default subscription found"
}
func determineTernantWithAzCliProfile() (string, error) {
path, err := cli.ProfilePath()
if err != nil {
return "", err
}
p, err := cli.LoadProfile(path)
if err != nil {
return "", err
}
for _, s := range p.Subscriptions {
if s.IsDefault {
return s.TenantID, nil
}
}
return "", &ErrNoDefaultSubscriptionFound{}
}
const authEndpointPrefix = "https://login.microsoftonline.com/"
var defaultClientId string
func consoleDo(tenantId, clientId string, callback func(context.Context, *CloudConsole) error) (string, int) {
tx := &http.Transport{
MaxIdleConns: 2,
IdleConnTimeout: 10 * time.Second,
}
authority := fmt.Sprintf("%s%s", authEndpointPrefix, url.PathEscape(tenantId))
cd := configdir.New("", "az-cloud-shell-access")
var credsPath string
{
c := cd.QueryFolders(configdir.Cache)[0]
err := os.MkdirAll(c.Path, 0700)
if err != nil {
return "failed to create cache directory", 1
}
credsPath = filepath.Join(c.Path, "accessTokens.json")
}
ctx := context.Background()
c, err := oauth2.NewClient(
tx, clientId,
oauth2.WithConfigurationOverrides(
oauth2.OpenIDConfiguration{
TokenEndpoint: authEndpointPrefix + "common/oauth2/token",
DeviceAuthorizationEndpoint: authEndpointPrefix + "common/oauth2/devicecode",
},
),
oauth2.WithResource("https://management.core.windows.net/"),
oauth2.WithFlow(
oauth2.NewLegacyDeviceCodeFlow(
func(msg string) error {
fmt.Println(msg)
return nil
},
),
),
oauth2.WithNowGetter(time.Now),
oauth2.WithTokenStore(
oauth2.NewSimpleTokenStore(credsPath, authority, time.Now),
),
)
if err != nil {
return err.Error(), 1
}
_, err = c.FetchTokens(ctx, false)
if err != nil {
return err.Error(), 1
}
console, err := NewCloudConsole(
"https://management.azure.com/",
tx,
func() (*oauth2.Tokens, error) {
return c.FetchTokens(ctx, true)
},
time.Now,
message,
)
if err != nil {
return err.Error(), 1
}
err = callback(ctx, console)
if err != nil {
return err.Error(), 2
}
return "", 0
}
var tenantId string
var clientId string
func checkTenantIdAndClientId() {
if clientId == "" {
clientId = defaultClientId
}
if clientId == "" {
message("no default client ID is provided, and AZURE_CLIENT_ID environment variable is not set.")
os.Exit(1)
}
if tenantId == "" {
message("no default subscription found in Azure CLI profile, and AZURE_TENANT_ID environment variable is not set.")
os.Exit(1)
}
}
var rootCmd = &cobra.Command{
Use: progName,
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&tenantId, "tenant-id", "", "Azure Tenant ID")
rootCmd.PersistentFlags().StringVar(&clientId, "client-id", "", "Azure Client ID")
{
var envVars []string
shellCmd := &cobra.Command{
Use: "shell",
Short: "start interative shell",
Run: func(cmd *cobra.Command, args []string) {
checkTenantIdAndClientId()
envVarsMap := make(map[string]string)
for _, envVar := range envVars {
pair := strings.SplitN(envVar, "=", 2)
k := pair[0]
v := ""
if len(pair) > 1 {
v = pair[1]
}
envVarsMap[k] = v
}
msg, status := consoleDo(tenantId, clientId, func(ctx context.Context, console *CloudConsole) error {
defer message("connection closed")
return console.Start(ctx, &TerminalReadWriter{os.Stdin}, envVarsMap)
})
if msg != "" {
message(msg)
}
os.Exit(status)
},
}
shellCmd.Flags().StringSliceVarP(&envVars, "env", "e", nil, "environment variable in NAME=VALUE format")
rootCmd.AddCommand(shellCmd)
}
{
var port int
var localAddr string
proxyCmd := &cobra.Command{
Use: "proxy",
Short: "start HTTP proxy",
Run: func(cmd *cobra.Command, args []string) {
if localAddr == "" {
localAddr = fmt.Sprintf("[::1]:%d", port)
}
checkTenantIdAndClientId()
msg, status := consoleDo(tenantId, clientId, func(ctx context.Context, console *CloudConsole) error {
return console.Proxy(
ctx, port, localAddr,
func(ctx context.Context, port int, localAddr string) error {
message(fmt.Sprintf("proxying port %d => %s", port, localAddr))
return nil
},
func(_ int, _ string) {
message("proxy port closed")
},
)
})
if msg != "" {
message(msg)
}
os.Exit(status)
},
}
proxyCmd.Flags().IntVar(&port, "port", 8091, "port to proxy")
proxyCmd.Flags().StringVar(&localAddr, "local-addr", "", "local address")
rootCmd.AddCommand(proxyCmd)
}
}
func initConfig() {
tenantId = os.Getenv("AZURE_TENANT_ID")
clientId = os.Getenv("AZURE_CLIENT_ID")
if tenantId == "" {
tenantId, _ = determineTernantWithAzCliProfile()
}
}
func main() {
err := rootCmd.Execute()
if err != nil {
message(err.Error())
os.Exit(1)
}
}