Skip to content

Commit

Permalink
follow next link for azure subscriptions API (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
vistaarjuneja authored Jan 25, 2024
1 parent 4672874 commit c2f00d6
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions cmd/kaniko-acr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
ACRCertPath = "/kaniko/acr-cert.pem"
pluginVersion = "unknown"
username = "00000000-0000-0000-0000-000000000000"
maxPageCount = 1000 // maximum count of pages to cycle through before we break out
)

func main() {
Expand Down Expand Up @@ -407,41 +408,56 @@ func getPublicUrl(token, registryUrl, subscriptionId string) (string, error) {
}

registry := strings.Split(registryUrl, ".")[0]
burl := "https://management.azure.com/subscriptions/" +
baseURL := "https://management.azure.com/subscriptions/" +
subscriptionId + "/resources?$filter=resourceType%20eq%20'Microsoft.ContainerRegistry/registries'%20and%20name%20eq%20'" +
registry + "'&api-version=2021-04-01&$select=id"

method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, burl, nil)
if err != nil {
fmt.Println(err)
return "", errors.Wrap(err, "failed to create request for getting container registry setting")
}

req.Header.Add("Authorization", "Bearer "+token)
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return "", errors.Wrap(err, "failed to send request for getting container registry setting")
}
defer res.Body.Close()
cnt := 0

var response strct
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return "", errors.Wrap(err, "failed to send request for getting container registry setting")
}
for {
// this is just in case we end up cycling through nextLink's infinitely.
// this should not happen - added as a precaution.
if cnt > maxPageCount {
break
}
cnt++
req, err := http.NewRequest(method, baseURL, nil)
if err != nil {
return "", errors.Wrap(err, "failed to create request for getting container registry setting")
}

if len(response.Value) == 0 {
return "", errors.New("did not receive any registry information from /subscriptions API")
}
req.Header.Add("Authorization", "Bearer "+token)
res, err := client.Do(req)
if err != nil {
return "", errors.Wrap(err, "failed to send request for getting container registry setting")
}
defer res.Body.Close()

var response strct
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return "", errors.Wrap(err, "failed to send request for getting container registry setting")
}

if len(response.Value) > 0 {
if response.Value[0].ID == "" { // should not happen
return "", errors.New("received empty registry ID from /subscriptions API")
}
return finalUrl + encodeParam(response.Value[0].ID), nil
}

if response.NextLink == "" {
// No more pages, break the loop
break
}

if response.Value[0].ID == "" {
return "", errors.New("received empty registry ID from /subscriptions API")
baseURL = response.NextLink
}

return finalUrl + encodeParam(response.Value[0].ID), nil
return "", errors.New("did not receive any registry information from /subscriptions API")
}

func encodeParam(s string) string {
Expand All @@ -452,4 +468,5 @@ type strct struct {
Value []struct {
ID string `json:"id"`
} `json:"value"`
NextLink string `json:"nextLink"` // for pagination
}

0 comments on commit c2f00d6

Please sign in to comment.