-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathurlutils.go
46 lines (36 loc) · 976 Bytes
/
urlutils.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
package main
import (
"errors"
"fmt"
"net/url"
"strings"
)
const (
armEndpoint string = "https://management.azure.com"
armEndpointSuffix string = "management.azure.com"
)
func isArmURLPath(urlPath string) bool {
urlPath = strings.ToLower(urlPath)
return strings.HasPrefix(urlPath, "/subscriptions") ||
strings.HasPrefix(urlPath, "/tenants") ||
strings.HasPrefix(urlPath, "/providers")
}
func getRequestURL(path string) (string, error) {
u, err := url.ParseRequestURI(path)
if err != nil || !u.IsAbs() {
if !isArmURLPath(path) {
return "", errors.New("Url path specified is invalid")
}
return armEndpoint + path, nil
}
if u.Scheme != "https" {
return "", errors.New("Scheme must be https")
}
if !strings.HasSuffix(u.Hostname(), armEndpointSuffix) {
return "", fmt.Errorf("'%s' is not an ARM endpoint", u.Hostname())
}
if !isArmURLPath(u.Path) {
return "", fmt.Errorf("Url path '%s' is invalid", u.Path)
}
return path, nil
}