1 Star 0 Fork 0

powerpaas / machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tenantid.go 3.78 KB
一键复制 编辑 原始数据 按行查看 历史
Aaron.L.Xu 提交于 2017-02-10 13:39 . fix typos in project scope
package azureutil
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/docker/machine/drivers/azure/logutil"
"github.com/docker/machine/libmachine/log"
"github.com/Azure/go-autorest/autorest/azure"
)
// loadOrFindTenantID figures out the AAD tenant ID of the subscription by first
// looking at the cache file, if not exists, makes a network call to load it and
// cache it for future use.
func loadOrFindTenantID(env azure.Environment, subscriptionID string) (string, error) {
var tenantID string
log.Debug("Looking up AAD Tenant ID.", logutil.Fields{
"subs": subscriptionID})
// Load from cache
fp := tenantIDPath(subscriptionID)
b, err := ioutil.ReadFile(fp)
if err == nil {
tenantID = strings.TrimSpace(string(b))
log.Debugf("Tenant ID loaded from file: %s", fp)
} else if os.IsNotExist(err) {
log.Debugf("Tenant ID file not found: %s", fp)
} else {
return "", fmt.Errorf("Failed to load tenant ID file: %v", err)
}
// Handle cache miss
if tenantID == "" {
log.Debug("Making API call to find tenant ID")
t, err := findTenantID(env, subscriptionID)
if err != nil {
return "", err
}
tenantID = t
// Cache the result
if err := saveTenantID(fp, tenantID); err != nil {
return "", fmt.Errorf("Failed to save tenant ID: %v", err)
}
log.Debugf("Cached tenant ID to file: %s", fp)
}
log.Debug("Found AAD Tenant ID.", logutil.Fields{
"tenant": tenantID,
"subs": subscriptionID})
return tenantID, nil
}
// findTenantID figures out the AAD tenant ID of the subscription by making an
// unauthenticated request to the Get Subscription Details endpoint and parses
// the value from WWW-Authenticate header.
func findTenantID(env azure.Environment, subscriptionID string) (string, error) {
const hdrKey = "WWW-Authenticate"
c := subscriptionsClient(env.ResourceManagerEndpoint)
// we expect this request to fail (err != nil), but we are only interested
// in headers, so surface the error if the Response is not present (i.e.
// network error etc)
subs, err := c.Get(subscriptionID)
if subs.Response.Response == nil {
return "", fmt.Errorf("Request failed: %v", err)
}
// Expecting 401 StatusUnauthorized here, just read the header
if subs.StatusCode != http.StatusUnauthorized {
return "", fmt.Errorf("Unexpected response from Get Subscription: %v", err)
}
hdr := subs.Header.Get(hdrKey)
if hdr == "" {
return "", fmt.Errorf("Header %v not found in Get Subscription response", hdrKey)
}
// Example value for hdr:
// Bearer authorization_uri="https://login.windows.net/996fe9d1-6171-40aa-945b-4c64b63bf655", error="invalid_token", error_description="The authentication failed because of missing 'Authorization' header."
r := regexp.MustCompile(`authorization_uri=".*/([0-9a-f\-]+)"`)
m := r.FindStringSubmatch(hdr)
if m == nil {
return "", fmt.Errorf("Could not find the tenant ID in header: %s %q", hdrKey, hdr)
}
return m[1], nil
}
// saveTenantID performs an atomic write to the path with given tenantID contents.
func saveTenantID(path string, tenantID string) error {
var perm os.FileMode = 0600
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return fmt.Errorf("Failed to create directory %s: %v", dir, err)
}
f, err := ioutil.TempFile(dir, "tenantid")
if err != nil {
return fmt.Errorf("Failed to create temp file: %v", err)
}
defer f.Close()
fp := f.Name()
if _, err := f.Write([]byte(tenantID)); err != nil {
return fmt.Errorf("Failed to write tenant ID to file: %v", err)
}
f.Close()
// atomic move by rename
if err := os.Rename(fp, path); err != nil {
return fmt.Errorf("Failed to rename file. src=%s dst=%s error=%v", fp, path, err)
}
if err := os.Chmod(path, perm); err != nil {
return fmt.Errorf("Failed to chmod the file %s: %v", path, err)
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/machine.git
git@gitee.com:powerpaas/machine.git
powerpaas
machine
machine
v0.13.0-rancher5

搜索帮助

344bd9b3 5694891 D2dac590 5694891