|
| 1 | +# Oracle NoSQL Database Go SDK: authenticating inside Oracle Cloud OKE Workload containers |
| 2 | + |
| 3 | +The Oracle NoSQL go SDK supports running programs inside OKE Workload containers in OCI. To do |
| 4 | +so, you must add the OCI go SDK to your dependencies, add a short bit of code to your app, and set up OCI policies to enable access to NoSQL. |
| 5 | +These steps are detailed below. |
| 6 | + |
| 7 | +You should read the OCI documentation at [Granting Workloads Access to OCI Resources](https://docs.oracle.com/en-us/iaas/Content/ContEng/Tasks/contenggrantingworkloadaccesstoresources.htm#contengmanagingworkloads_topic-grantingworkloadaccesstoresources-golang). Also, this [tutorial](https://docs.oracle.com/en-us/iaas/developer-tutorials/tutorials/helidon-k8s/01oci-helidon-k8s-summary.htm) is helpful in understanding the steps to create and manage OCI OKE Workloads. |
| 8 | + |
| 9 | +## Add OCI go SDK to your dependencies |
| 10 | + |
| 11 | +To add the OC go SDK as a dependency to your project (if not already using it), run the following command in your top-level go module directory: |
| 12 | + |
| 13 | +``` |
| 14 | +go get github.com/oracle/oci-go-sdk/v65@latest |
| 15 | +``` |
| 16 | + |
| 17 | +## Add code to your app to use OKE Workload identity |
| 18 | + |
| 19 | +The NoSQL go SDK has a slightly enhanced version of `ConfigurationProvider` interface than the OCI go SDK. As such, there is a bit of extra code needed to use the OKE workload configuration provider in a NoSQL in your app. |
| 20 | + |
| 21 | +First, create a new struct and 2 methods that wrap the OCI `ConfigurationProviderWithClaimAccess`: |
| 22 | + |
| 23 | +``` |
| 24 | +import ociauth "github.com/oracle/oci-go-sdk/v65/common/auth" |
| 25 | +
|
| 26 | +
|
| 27 | +type myConfigProvider struct { |
| 28 | + ociauth.ConfigurationProviderWithClaimAccess |
| 29 | +} |
| 30 | +
|
| 31 | +// ExpirationTime in this case always returns a time in the future, effectively setting no expiration. |
| 32 | +func (c *myConfigProvider) ExpirationTime() time.Time { |
| 33 | + return time.Now().Add(time.Hour) |
| 34 | +} |
| 35 | +
|
| 36 | +// SecurityTokenFile is not applicable to the OKE Workload case |
| 37 | +func (c *myConfigProvider) SecurityTokenFile() (string, error) { |
| 38 | + return "", fmt.Errorf("myConfigurationProvider does not support SecurityTokenFile") |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Then, in your code that creates a SignatureProvider to use with NoSQL, Add the following code to use OKE Workload identity: |
| 43 | +``` |
| 44 | + conf, err := ociauth.OkeWorkloadIdentityConfigurationProvider() |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("cannot create an Oke Provider: %v", err) |
| 47 | + } |
| 48 | + myconf := &myConfigProvider{conf} |
| 49 | + sp, err := iam.NewSignatureProviderWithConfiguration(myconf, compartmentID) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) |
| 52 | + } |
| 53 | +``` |
| 54 | + |
| 55 | +## Create OCI Identity Policy to allow NoSQL access from your OKE Workload |
| 56 | + |
| 57 | +This part follows the same pattern as described in |
| 58 | + [Granting Workloads Access to OCI Resources](https://docs.oracle.com/en-us/iaas/Content/ContEng/Tasks/contenggrantingworkloadaccesstoresources.htm#contengmanagingworkloads_topic-grantingworkloadaccesstoresources-golang). Here's an example policy to use as a guide: |
| 59 | +``` |
| 60 | +Allow any-user to manage nosql-family in compartment app_compartment where all { |
| 61 | + request.principal.type = 'workload', |
| 62 | + request.principal.namespace = 'default', |
| 63 | + request.principal.service_account = 'app-testaccount', |
| 64 | + request.principal.cluster_id = 'ocid1.cluster.oc1.___________________________'i |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Once complete, you should be able to build your app as usual, and deploy it to OKE Workload using `docker` and `kubectl` commands as described in the above-referenced [tutorial](https://docs.oracle.com/en-us/iaas/developer-tutorials/tutorials/helidon-k8s/01oci-helidon-k8s-summary.htm). |
| 69 | + |
| 70 | +## Special note |
| 71 | + |
| 72 | +Note: as of this writing, the OCI go SDK has a small incompatibility with OKE Workloads. See [this issue report](https://github.com/oracle/oci-go-sdk/issues/489) for more details. |
| 73 | +In short, you need to add two environment settings for the auth to work correctly. They can be in the Dockerfile, in `os.Setenv()` calls, or however else you wish to set the ennvironment for your app. Here's an example of the two values to set in a Dockerfile: |
| 74 | +``` |
| 75 | +ENV OCI_RESOURCE_PRINCIPAL_VERSION 2.2 |
| 76 | +ENV OCI_RESOURCE_PRINCIPAL_REGION us-ashburn-1 |
| 77 | +``` |
| 78 | + |
0 commit comments