-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathexample_securityTokenBasedAuth_test.go
68 lines (55 loc) · 2.32 KB
/
example_securityTokenBasedAuth_test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2016, 2018, 2025, Oracle and/or its affiliates. All rights reserved.
// This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
// Example code for using security token based auth
package example
import (
"context"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"github.com/oracle/oci-go-sdk/v65/common"
"github.com/oracle/oci-go-sdk/v65/example/helpers"
"github.com/oracle/oci-go-sdk/v65/identity"
)
// This test requires that you have a specified profile setup for security-token based authentication, for the detail
// how to set up the configuration, please refer https://docs.oracle.com/iaas/en-us/iaas/Content/API/SDKDocs/clitoken.htm?Highlight=security_token_file
// In this example the [security_token_based_auth] is created and lists Lists the Availability Domains in current tenancy.
// The token will expire in 1 hour, user needs to check if the token is valid and then decide refresh steps via OCI CLI command.
const (
profileName = "security_token_based_auth"
cfgDirName = ".oci"
cfgFileName = "config"
)
func ExampleCreateAndUseSecurityTokenBasedConfiguration() {
homeFolder := getHomeFolder()
configFilePath := filepath.Join(homeFolder, cfgDirName, cfgFileName)
securityTokenBasedAuthConfigProvider := common.CustomProfileConfigProvider(configFilePath, profileName)
c, err := identity.NewIdentityClientWithConfigurationProvider(securityTokenBasedAuthConfigProvider)
helpers.FatalIfError(err)
// The OCID of the tenancy containing the compartment.
tenancyID, err := securityTokenBasedAuthConfigProvider.TenancyOCID()
helpers.FatalIfError(err)
request := identity.ListAvailabilityDomainsRequest{
CompartmentId: &tenancyID,
}
r, err := c.ListAvailabilityDomains(context.Background(), request)
helpers.FatalIfError(err)
log.Printf("list of available domains: %v", r.Items)
fmt.Println("list available domains completed")
// Output:
// list available domains completed
}
func getHomeFolder() string {
current, e := user.Current()
if e != nil {
//Give up and try to return something sensible
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return current.HomeDir
}