Skip to content

Commit b166349

Browse files
SaschaSchwarze0reggeenr
authored andcommitted
Add samples for how to use trusted profiles
Signed-off-by: Sascha Schwarze <[email protected]>
1 parent f9d933f commit b166349

23 files changed

+1488
-0
lines changed

trusted-profiles/README.md

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# Trusted Profiles
2+
3+
In the IBM Cloud, when authenticating with other services such as Cloud
4+
Object Storage or Secrets Manager, using trusted profiles is a way to
5+
authenticate without any API keys being used. This eliminates the risk of
6+
those being leaked or stolen by a malicious user who uses them to access your
7+
IBM Cloud resources.
8+
9+
You can read more about trusted profiles in the
10+
[IBM Cloud documentation](https://cloud.ibm.com/docs/account?topic=account-create-trusted-profile).
11+
12+
The samples in this directory show how to use the trusted profile in your code
13+
using the IBM Cloud SDK that exists for Go, Java, Node, and Python.
14+
15+
If you are using a different programming language, then you can still use
16+
trusted profiles, but must implement the interaction with
17+
[IAM to retrieve an access token for a compute resource token](https://cloud.ibm.com/apidocs/iam-identity-token-api#gettoken-crtoken)
18+
yourself.
19+
20+
Each of the samples then uses the token to list the files in a Cloud Object
21+
Storage bucket.
22+
23+
Following are the steps to run the sample. Please note that the sample
24+
requires account permissions to create various resources including those
25+
related to IAM permissions.
26+
27+
## Setup
28+
29+
To setup the example, you need three things:
30+
31+
1. A Code Engine project
32+
2. A Cloud Object Storage bucket
33+
3. A trusted profile that grants access to the Cloud Object Storage bucket
34+
35+
### Creating a Code Engine project
36+
37+
We are using the command line interface here to setup things. If you have not
38+
used it before, you can directly run it through the
39+
[IBM Cloud shell](https://cloud.ibm.com/shell).
40+
41+
When running locally, make sure the necessary plugin is installed:
42+
43+
```sh
44+
ibmcloud plugin install code-engine
45+
```
46+
47+
In the IBM Cloud shell, the plugin is already installed, however it makes
48+
sense to ensure the latest version is installed:
49+
50+
```sh
51+
ibmcloud plugin update --all --force
52+
```
53+
54+
Then you are ready to create a Code Engine project. Here and in the following
55+
snippets, variables will be used. Feel free to adjust them to your needs, but
56+
make sure that you use the same value for the same variable in all snippets.
57+
58+
```sh
59+
REGION=eu-es
60+
RESOURCE_GROUP=Default
61+
CE_PROJECT_NAME=trusted-profiles-test
62+
63+
ibmcloud target -r ${REGION} -g ${RESOURCE_GROUP}
64+
ibmcloud ce project create --name ${CE_PROJECT_NAME}
65+
```
66+
67+
### Creating a Cloud Object Storage bucket
68+
69+
For this sample, you can use an existing Cloud Object Storage bucket that
70+
you already have. If you never used COS, then here is a one-sentence
71+
introduction: Cloud Object Storage is a managed data service where you can
72+
store data in files.
73+
74+
With the following commands, you will setup your first COS instance and a
75+
bucket. First, make sure the CLI plugin is installed:
76+
77+
```sh
78+
ibmcloud plugin install cos
79+
```
80+
81+
The COS bucket uses a random suffix (`31292`) because bucket names must
82+
be unique across all IBM Cloud customers in a region. Make sure you use
83+
your own random characters.
84+
85+
```sh
86+
REGION=eu-es
87+
RESOURCE_GROUP=Default
88+
COS_INSTANCE_NAME=my-first-cos
89+
COS_BUCKET=my-first-bucket-31292
90+
91+
ibmcloud resource service-instance-create ${COS_INSTANCE_NAME} cloud-object-storage standard global -g ${RESOURCE_GROUP} -d premium-global-deployment-iam
92+
COS_INSTANCE_ID=$(ibmcloud resource service-instance ${COS_INSTANCE_NAME} --crn 2>/dev/null | grep ':cloud-object-storage:')
93+
ibmcloud cos config crn --crn ${COS_INSTANCE_ID} --force
94+
ibmcloud cos bucket-create --bucket ${COS_BUCKET} --class smart --ibm-service-instance-id ${COS_INSTANCE_ID} --region ${REGION}
95+
```
96+
97+
To have content in the bucket, let's store a sample text file:
98+
99+
```sh
100+
echo Hello World >helloworld.txt
101+
ibmcloud cos object-put --region ${REGION} --bucket ${COS_BUCKET} --key helloworld.txt --body helloworld.txt
102+
```
103+
104+
### Creating a trusted profile that grants a Code Engine Job access to your COS bucket
105+
106+
In this step, we are creating a Trusted Profile which grants read access to
107+
your COS bucket to a Job called `list-cos-files` in your Code Engine project.
108+
109+
The Job itself, we will create later.
110+
111+
```sh
112+
REGION=eu-es
113+
RESOURCE_GROUP=Default
114+
COS_INSTANCE_NAME=my-first-cos
115+
COS_BUCKET=my-first-bucket-31292
116+
CE_PROJECT_NAME=trusted-profiles-test
117+
JOB_NAME=list-cos-files
118+
TRUSTED_PROFILE_NAME=code-engine-cos-access
119+
120+
CE_PROJECT_CRN=$(ibmcloud resource service-instance ${CE_PROJECT_NAME} --location ${REGION} -g ${RESOURCE_GROUP} --crn 2>/dev/null | grep ':codeengine:')
121+
COS_INSTANCE_ID=$(ibmcloud resource service-instance ${COS_INSTANCE_NAME} --crn 2>/dev/null | grep ':cloud-object-storage:')
122+
123+
ibmcloud iam trusted-profile-create ${TRUSTED_PROFILE_NAME}
124+
ibmcloud iam trusted-profile-link-create ${TRUSTED_PROFILE_NAME} --name ce-job-${JOB_NAME} --cr-type CE --link-crn ${CE_PROJECT_CRN} --link-component-type job --link-component-name ${JOB_NAME}
125+
ibmcloud iam trusted-profile-policy-create ${TRUSTED_PROFILE_NAME} --roles "Content Reader" --service-name cloud-object-storage --service-instance ${COS_INSTANCE_ID} --resource-type bucket --resource ${COS_BUCKET}
126+
```
127+
128+
## Running the sample
129+
130+
To run the sample, we will now create the Code Engine Job pointing to the
131+
sources in this repository. Feel free to use `go`, `java`, `node` or
132+
`python` as value for the `PROGRAMMING_LANGUAGE` variable.
133+
134+
```sh
135+
REGION=eu-es
136+
COS_BUCKET=my-first-bucket-31292
137+
JOB_NAME=list-cos-files
138+
PROGRAMMING_LANGUAGE=node
139+
TRUSTED_PROFILE_NAME=code-engine-cos-access
140+
141+
ibmcloud ce job create --name ${JOB_NAME} \
142+
--build-source https://github.com/IBM/CodeEngine \
143+
--build-context-dir trusted-profiles/${PROGRAMMING_LANGUAGE} \
144+
--trusted-profiles-enabled true \
145+
--env COS_REGION=${REGION} \
146+
--env COS_BUCKET=${COS_BUCKET} \
147+
--env TRUSTED_PROFILE_NAME=${TRUSTED_PROFILE_NAME}
148+
```
149+
150+
Code Engine will setup the Job and as part of that runs a build of the chosen
151+
source. The output is a container image that it pushes to a Container Registry
152+
namespace that it creates for your project. Once that completed, your Job is
153+
ready to be run:
154+
155+
```sh
156+
JOB_NAME=list-cos-files
157+
158+
ibmcloud ce jobrun submit --job ${JOB_NAME} --name ${JOB_NAME}-run-1
159+
ibmcloud ce jobrun logs --name ${JOB_NAME}-run-1 --follow
160+
```
161+
162+
If everything has been setup correctly, the JobRun logs will show the number
163+
of items it found and their keys, in case you set up the sample bucket above,
164+
then it will be just one item called helloworld.txt.
165+
166+
## Code Review
167+
168+
All of the four samples use the language-specific IBM Cloud SDK. Those define
169+
an `Authenticator` interface and provide the `ContainerAuthenticator`
170+
implementation. When instantiating the `ContainerAuthenticator`, you must
171+
provide the identifier or name of trusted profile. The sample job from above
172+
uses the name which is defined as `TRUSTED_PROFILE_NAME` environment variable
173+
on the Code Engine Job.
174+
175+
The `Authenticator` has an `authenticate` method that augments an existing
176+
HTTP request object with the necessary `Authorization` header. Under the
177+
covers, the `ContainerAuthenticator` for that purpose reaches out to
178+
[IAM to retrieve an access token for a compute resource token](https://cloud.ibm.com/apidocs/iam-identity-token-api#gettoken-crtoken).
179+
180+
The `ContainerAuthenticator` will also automatically manage the refresh of the
181+
token.
182+
183+
The sample code authenticates a request against the Cloud Object Storage API
184+
to list the items in a bucket. The response is parsed and printed.

trusted-profiles/go/build

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
# Env Vars:
4+
# REGISTRY: name of the image registry/namespace to store the images
5+
#
6+
# NOTE: to run this you MUST set the REGISTRY environment variable to
7+
# your own image registry/namespace otherwise the `docker push` commands
8+
# will fail due to an auth failure. Which means, you also need to be logged
9+
# into that registry before you run it.
10+
11+
set -ex
12+
export REGISTRY=${REGISTRY:-icr.io/codeengine}
13+
14+
# Build and push the image
15+
KO_DOCKER_REPO="${REGISTRY}/trusted-profiles/go" ko build . --bare --image-user 1001 --platform linux/amd64 --sbom=none

trusted-profiles/go/go.mod

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module github.com/IBM/CodeEngine/trusted-profiles/go
2+
3+
go 1.23.0
4+
5+
require github.com/IBM/go-sdk-core/v5 v5.19.0
6+
7+
require (
8+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
9+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
10+
github.com/go-openapi/errors v0.22.0 // indirect
11+
github.com/go-openapi/strfmt v0.23.0 // indirect
12+
github.com/go-playground/locales v0.14.1 // indirect
13+
github.com/go-playground/universal-translator v0.18.1 // indirect
14+
github.com/go-playground/validator/v10 v10.25.0 // indirect
15+
github.com/google/uuid v1.6.0 // indirect
16+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
17+
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
18+
github.com/leodido/go-urn v1.4.0 // indirect
19+
github.com/mitchellh/mapstructure v1.5.0 // indirect
20+
github.com/oklog/ulid v1.3.1 // indirect
21+
github.com/stretchr/testify v1.10.0 // indirect
22+
go.mongodb.org/mongo-driver v1.17.3 // indirect
23+
golang.org/x/crypto v0.36.0 // indirect
24+
golang.org/x/net v0.37.0 // indirect
25+
golang.org/x/sys v0.31.0 // indirect
26+
golang.org/x/text v0.23.0 // indirect
27+
gopkg.in/yaml.v2 v2.4.0 // indirect
28+
)

trusted-profiles/go/go.sum

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
github.com/IBM/go-sdk-core/v5 v5.19.0 h1:YN2S5JUvq/EwYulmcNFwgyYBxZhVWl9nkY22H7Hpghw=
2+
github.com/IBM/go-sdk-core/v5 v5.19.0/go.mod h1:deZO1J5TSlU69bCnl/YV7nPxFZA2UEaup7cq/7ZTOgw=
3+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
4+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
5+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
8+
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
9+
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
10+
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
11+
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
12+
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
13+
github.com/go-openapi/errors v0.22.0 h1:c4xY/OLxUBSTiepAg3j/MHuAv5mJhnf53LLMWFB+u/w=
14+
github.com/go-openapi/errors v0.22.0/go.mod h1:J3DmZScxCDufmIMsdOuDHxJbdOGC0xtUynjIx092vXE=
15+
github.com/go-openapi/strfmt v0.23.0 h1:nlUS6BCqcnAk0pyhi9Y+kdDVZdZMHfEKQiS4HaMgO/c=
16+
github.com/go-openapi/strfmt v0.23.0/go.mod h1:NrtIpfKtWIygRkKVsxh7XQMDQW5HKQl6S5ik2elW+K4=
17+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
18+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
19+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
20+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
21+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
22+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
23+
github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8=
24+
github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
25+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
26+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
27+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
28+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
29+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
30+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
31+
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
32+
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
33+
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
34+
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
35+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
36+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
37+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
38+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
39+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
40+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
41+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
42+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
43+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
44+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
45+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
46+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
47+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
48+
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
49+
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
50+
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
51+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
52+
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
53+
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
54+
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
55+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
56+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
58+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
59+
go.mongodb.org/mongo-driver v1.17.3 h1:TQyXhnsWfWtgAhMtOgtYHMTkZIfBTpMTsMnd9ZBeHxQ=
60+
go.mongodb.org/mongo-driver v1.17.3/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
61+
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
62+
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
63+
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
64+
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
65+
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
66+
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
67+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
68+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
69+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
70+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
71+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
72+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
73+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
74+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
75+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
76+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
77+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

trusted-profiles/go/main.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"io"
7+
"log"
8+
"net/http"
9+
"os"
10+
11+
"github.com/IBM/go-sdk-core/v5/core"
12+
)
13+
14+
type content struct {
15+
Key string `xml:"Key"`
16+
}
17+
18+
type listBucketResult struct {
19+
Contents []content `xml:"Contents"`
20+
}
21+
22+
func main() {
23+
// read environment variables
24+
cosBucket := os.Getenv("COS_BUCKET")
25+
if cosBucket == "" {
26+
log.Panic("environment variable COS_BUCKET is not set")
27+
}
28+
cosRegion := os.Getenv("COS_REGION")
29+
if cosRegion == "" {
30+
log.Panic("environment variable COS_REGION is not set")
31+
}
32+
trustedProfileName := os.Getenv("TRUSTED_PROFILE_NAME")
33+
if trustedProfileName == "" {
34+
log.Panic("environment variable TRUSTED_PROFILE_NAME is not set")
35+
}
36+
37+
// create an authenticator based on a trusted profile
38+
authenticator := core.NewContainerAuthenticatorBuilder().SetIAMProfileName(trustedProfileName)
39+
40+
// prepare the request to list the files in the bucket
41+
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://s3.direct.%s.cloud-object-storage.appdomain.cloud/%s", cosRegion, cosBucket), nil)
42+
if err != nil {
43+
log.Panicf("Failed to create request: %v", err)
44+
}
45+
46+
// authenticate the request
47+
if err = authenticator.Authenticate(request); err != nil {
48+
log.Panicf("Failed to authenticate request: %v", err)
49+
}
50+
51+
// perform the request
52+
response, err := http.DefaultClient.Do(request)
53+
if err != nil {
54+
log.Panicf("Failed to perform request: %v", err)
55+
}
56+
defer response.Body.Close()
57+
if response.StatusCode != http.StatusOK {
58+
log.Panicf("Unexpected status code: %d", response.StatusCode)
59+
}
60+
61+
body, err := io.ReadAll(response.Body)
62+
if err != nil {
63+
log.Panicf("Failed to read response body: %v", err)
64+
}
65+
66+
// parse the response which is in XML format
67+
listBucketResult := &listBucketResult{}
68+
if err = xml.Unmarshal(body, listBucketResult); err != nil {
69+
log.Panicf("Failed to parse response body: %v", err)
70+
}
71+
72+
// print the details
73+
log.Printf("Found %d objects:", len(listBucketResult.Contents))
74+
for _, item := range listBucketResult.Contents {
75+
log.Printf("- %s", item.Key)
76+
}
77+
}

trusted-profiles/java/.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.dockerignore
2+
.gitignore
3+
build
4+
Dockerfile
5+
target

trusted-profiles/java/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

0 commit comments

Comments
 (0)