Skip to content

Commit c43902a

Browse files
committed
Remake into basic REST calls
1 parent 15f2ddd commit c43902a

File tree

4 files changed

+39
-70
lines changed

4 files changed

+39
-70
lines changed

cmd/ciCloudLogin.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,7 @@ var cloudLoginCmd = &cobra.Command{
253253
_ = os.Mkdir(filepath.Dir(kubeConfigPath), 0750)
254254
}
255255

256-
ctx, cred, err := az.GetAzureCredentials(aksTenantID)
257-
if err != nil {
258-
log.Fatalf("Error: %s", err)
259-
}
260-
261-
config, err := az.GetKubeconfig(ctx, *cred, aksResourceGroup, subscriptionID, clusterName)
256+
config, err := az.GetKubeconfig(token, subscriptionID, aksResourceGroup, clusterName)
262257
if err != nil {
263258
log.Fatalf("Error: %s", err)
264259
}

go.mod

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,14 @@ require (
99
)
1010

1111
require (
12-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2
13-
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0
1412
github.com/mittwald/go-helm-client v0.11.3
1513
k8s.io/apimachinery v0.24.3
1614
k8s.io/client-go v0.24.3
1715
)
1816

19-
require (
20-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.5.0 // indirect
21-
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
22-
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
23-
)
24-
2517
require (
2618
cloud.google.com/go v0.99.0 // indirect
2719
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
28-
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 // indirect
2920
github.com/BurntSushi/toml v1.0.0 // indirect
3021
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
3122
github.com/Masterminds/goutils v1.1.1 // indirect
@@ -61,7 +52,6 @@ require (
6152
github.com/go-openapi/swag v0.19.14 // indirect
6253
github.com/gobwas/glob v0.2.3 // indirect
6354
github.com/gogo/protobuf v1.3.2 // indirect
64-
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
6555
github.com/golang/protobuf v1.5.2 // indirect
6656
github.com/google/btree v1.0.1 // indirect
6757
github.com/google/gnostic v0.6.9 // indirect
@@ -79,7 +69,6 @@ require (
7969
github.com/josharian/intern v1.0.0 // indirect
8070
github.com/json-iterator/go v1.1.12 // indirect
8171
github.com/klauspost/compress v1.13.6 // indirect
82-
github.com/kylelemons/godebug v1.1.0 // indirect
8372
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
8473
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
8574
github.com/lib/pq v1.10.6 // indirect

go.sum

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
4646
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
4747
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
4848
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
49-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.5.0 h1:xGLAFFd9D3iLGxYiUGPdITSzsFmU1K8VtfuUHWAoN7M=
50-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.5.0/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q=
51-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2 h1:uqM+VoHjVH6zdlkLF2b6O0ZANcHoj3rO0PoQ3jglUJA=
52-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2/go.mod h1:twTKAa1E6hLmSDjLhaCkbTMQKc7p/rNLU40rLxGEOCI=
53-
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 h1:sXr+ck84g/ZlZUOZiNELInmMgOsuGwdjjVkEIde0OtY=
54-
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
55-
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0 h1:figxyQZXzZQIcP3njhC68bYUiTw45J8/SsHaLW8Ax0M=
56-
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0/go.mod h1:TmlMW4W5OvXOmOyKNnor8nlMMiO1ctIyzmHme/VHsrA=
5749
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
5850
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
5951
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
@@ -63,8 +55,6 @@ github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSY
6355
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
6456
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
6557
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
66-
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 h1:UE9n9rkJF62ArLb1F3DEjRt8O3jLwMWdSoypKV4f3MU=
67-
github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
6858
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
6959
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
7060
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
@@ -176,7 +166,6 @@ github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27N
176166
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
177167
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
178168
github.com/distribution/distribution/v3 v3.0.0-20220526142353-ffbd94cbe269 h1:hbCT8ZPPMqefiAWD2ZKjn7ypokIGViTvBBg/ExLSdCk=
179-
github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c=
180169
github.com/docker/cli v20.10.17+incompatible h1:eO2KS7ZFeov5UJeaDmIs1NFEDRf32PaqRpvoEkKBy5M=
181170
github.com/docker/cli v20.10.17+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
182171
github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68=
@@ -279,8 +268,6 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
279268
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
280269
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
281270
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
282-
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
283-
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
284271
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
285272
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
286273
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
@@ -461,8 +448,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
461448
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
462449
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
463450
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
464-
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
465-
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
466451
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
467452
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
468453
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
@@ -577,8 +562,6 @@ github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
577562
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
578563
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
579564
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
580-
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
581-
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
582565
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
583566
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
584567
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -951,7 +934,6 @@ golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBc
951934
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
952935
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
953936
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
954-
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
955937
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
956938
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
957939
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

internal/azure/ciAuth.go

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package azure
22

33
import (
4-
"context"
4+
b64 "encoding/base64"
55
"encoding/json"
66
"errors"
77
"io"
88
"net/http"
99
"strings"
10-
11-
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
12-
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice"
1310
)
1411

12+
type Base64Kubeconfig struct {
13+
Base64Kubeconfig string `json:"value"`
14+
Name string `json:"name"`
15+
}
16+
1517
type Subscription struct {
1618
SubscriptionID string `json:"subscriptionId"`
1719
}
@@ -24,37 +26,6 @@ type tokenResponse struct {
2426
ExtExpiresIn float64 `json:"ext_expires_in"`
2527
}
2628

27-
func GetAzureCredentials(tenantID string) (ctx context.Context, cred *azidentity.DefaultAzureCredential, err error) {
28-
29-
cred, err = azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{TenantID: tenantID})
30-
if err != nil {
31-
return nil, nil, err
32-
}
33-
34-
ctx = context.Background()
35-
return ctx, cred, nil
36-
}
37-
38-
func GetKubeconfig(ctx context.Context, cred azidentity.DefaultAzureCredential, resourceGroupName string, subscriptionID string, clusterName string) (kubeconfig []byte, err error) {
39-
40-
mcClient, err := armcontainerservice.NewManagedClustersClient(subscriptionID, &cred, nil)
41-
if err != nil {
42-
return nil, err
43-
}
44-
45-
resp, err := mcClient.ListClusterAdminCredentials(ctx, resourceGroupName, clusterName, nil)
46-
if err != nil {
47-
return nil, err
48-
}
49-
50-
if len(resp.Kubeconfigs) > 0 {
51-
return resp.Kubeconfigs[0].Value, nil
52-
} else {
53-
return nil, errors.New("no kubeconfigs are available")
54-
}
55-
56-
}
57-
5829
// Returns the first subscription ID
5930
// If no IDs are found, 'empty' is set to true
6031
func GetDefaultSubscriptionID(token string) (subscriptionID string, err error) {
@@ -120,3 +91,35 @@ func GetAuthToken(tenantId string, clientId string, clientSecret string) (string
12091
}
12192
return token.AccessToken, nil
12293
}
94+
95+
func GetKubeconfig(accessToken string, subscriptionId string, resourceGroupName string, clusterName string) ([]byte, error) {
96+
req, err := http.NewRequest(http.MethodPost, "https://management.azure.com/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroupName+"/providers/Microsoft.ContainerService/managedClusters/"+clusterName+"/listClusterAdminCredential?api-version=2023-02-01", nil)
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
req.Header.Set("Authorization", "Bearer "+accessToken)
102+
103+
resp, err := http.DefaultClient.Do(req)
104+
if err != nil {
105+
return nil, err
106+
}
107+
defer resp.Body.Close()
108+
109+
var kubeconfigs struct {
110+
Value []Base64Kubeconfig `json:"kubeconfigs"`
111+
}
112+
if err := json.NewDecoder(resp.Body).Decode(&kubeconfigs); err != nil {
113+
return nil, err
114+
}
115+
116+
if len(kubeconfigs.Value) == 0 {
117+
return nil, errors.New("no kubeconfigs found")
118+
}
119+
120+
kconfigByte, err := b64.StdEncoding.DecodeString(kubeconfigs.Value[0].Base64Kubeconfig)
121+
if err != nil {
122+
return nil, err
123+
}
124+
return kconfigByte, nil
125+
}

0 commit comments

Comments
 (0)