Esempio n. 1
0
func main() {
	log.Println("loading certificate... ")
	certData, err := ioutil.ReadFile(certificatePath)
	if err != nil {
		log.Fatalln("failed", err)
	}

	log.Println("retrieve oauth token... ")
	spt, err := azure.NewServicePrincipalTokenFromCertificate(
		applicationID,
		certData,
		"",
		tenantID,
		azure.AzureResourceManagerScope)
	if err != nil {
		log.Fatalln("failed", err)
		panic(err)
	}

	client := &autorest.Client{}
	client.Authorizer = spt

	log.Println("querying the list of resource groups... ")
	groupsAsString, err := getResourceGroups(client)
	if err != nil {
		log.Fatalln("failed", err)
	}

	log.Println("")
	log.Println("Groups:", *groupsAsString)
}
Esempio n. 2
0
func getSptFromCertificate(oauthConfig azure.OAuthConfig, clientID, resource, certicatePath string, callbacks ...azure.TokenRefreshCallback) (*azure.ServicePrincipalToken, error) {
	certData, err := ioutil.ReadFile(certificatePath)
	if err != nil {
		return nil, fmt.Errorf("failed to read the certificate file (%s): %v", certificatePath, err)
	}

	certificate, rsaPrivateKey, err := decodePkcs12(certData, "")
	if err != nil {
		return nil, fmt.Errorf("failed to decode pkcs12 certificate while creating spt: %v", err)
	}

	spt, _ := azure.NewServicePrincipalTokenFromCertificate(
		oauthConfig,
		clientID,
		certificate,
		rsaPrivateKey,
		resource,
		callbacks...)

	return spt, nil
}