Skip to content

Commit 8aa94d7

Browse files
authored
Merge pull request #1580 from oracle-devrel/oci-devops-aifunction-fix
auth mods
2 parents 3436c44 + 9fc8c36 commit 8aa94d7

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,35 @@ This example is based on the <a href="../java-helloworld-with-local-dev-and-oci-
5353

5454
<p>
5555

56-
To do the OCI SDK authentication and authorization to use the GenAI services the function uses two options:
56+
To do the OCI SDK authentication and authorization to use the GenAI services the function has three options:
5757
<ul>
58-
<li><b>IAM regular user</b> for the local dev and test on mac (lines 79-84 in HelloAIFunction.java)</li>
59-
<li><b>ResourcePrincipal</b> for the OCI Function</li>
58+
<li><b>ResourcePrincipal</b> for the OCI Function to run in OCI. This allows Function to be authorized as part of
59+
a OCI Dynamic Group that has OCI Policies attached to for the Function to do it's job.</li>
60+
<li><b>IAM regular user</b> for the local dev and test on mac and passing the vars in source code (lines 79-84 in HelloAIFunction.java). This works for testing locally but the container should not be distributed!</li>
61+
<li><b>IAM regular user</b> for the local dev and test on mac using OCI CLI config file (usually located in ~/.oci). Again, this works for testing locally but the container should not be distributed!</li>
6062
</ul>
6163

6264
<p>
6365
IAM user option will work on both cases above, as local and as OCI Function. ResourcePrincipal is the default for OCI Function.
66+
<p>
6467

6568
## Build and test
6669

6770
Following the steps of the <a href="../java-helloworld-with-local-dev-and-oci-functions">Hello function example </a> adjust the <a href="https://github.com/oracle-devrel/technology-engineering/blob/main/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/files/src/main/java/com/example/HelloAIFunction.java#76">line 76</a> to match your <code>compartment OCID</code> and the <a href="https://github.com/oracle-devrel/technology-engineering/blob/main/app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/files/src/main/java/com/example/HelloAIFunction.java#77">line 77</a> to match your <code>GenAI service model OCID</code>.
6871

6972
<p>
7073

74+
To use <code>.oci config</code> for testing locally replace the contents of Dockerfile with the contents from <a href="Dockerfile.local_oci">Dockerfile.local_oci</a>. Then copy your <code>~/.oci</code> -directory under the project root and build the Function with Fn:
75+
76+
<pre>
77+
fn --verbose deploy --app hellofunction --local
78+
fn invoke hellofunction helloaifunc
79+
</pre>
80+
81+
<i>Note! Do not distribute this container since it contains your OCI credentials. Use this only for local testing purposes.</i>
82+
83+
<p>
84+
7185
Testing with curl (or copy-pasting the API Gateway deployment url to a browser):
7286

7387
<p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM fnproject/fn-java-fdk-build:jdk17-1.0-latest as build-stage
2+
WORKDIR /function
3+
ENV MAVEN_OPTS -Dhttp.proxyHost= -Dhttp.proxyPort= -Dhttps.proxyHost= -Dhttps.proxyPort= -Dhttp.nonProxyHosts= -Dmaven.repo.local=/usr/share/maven/ref/repository
4+
ADD pom.xml /function/pom.xml
5+
RUN ["mvn", "package", "dependency:copy-dependencies", "-DincludeScope=runtime", "-DskipTests=true", "-Dmdep.prependGroupId=true", "-DoutputDirectory=target", "--fail-never"]
6+
ADD src /function/src
7+
RUN ["mvn", "package"]
8+
FROM fnproject/fn-java-fdk:jre17-1.0.187
9+
WORKDIR /function
10+
COPY --from=build-stage /function/target/*.jar /function/app/
11+
RUN echo "**** WARNING ***"
12+
RUN echo "**** THIS CONTAINER CONTAINS OCI CREDENTIALS - DO NOT DISTRIBUTE ***"
13+
RUN echo "Copy your OCI CLI .oci dir under this dir before running this Dockerfile "
14+
RUN echo "OCI API KEYFILE is expected to be without any path in the config e.g. key_file = oci_api_key.pem"
15+
ADD .oci/config /
16+
ADD .oci/oci_api_key.pem /
17+
RUN chmod 777 /config
18+
RUN chmod 777 /oci_api_key.pem
19+
RUN sed -i '/^key_file/d' /config
20+
RUN echo "key_file = /oci_api_key.pem" >> /config
21+
CMD ["com.example.HelloAIFunction::handleRequest"]

app-dev/devops-and-containers/functions/java-helloworld-AI-with-local-dev-and-oci-functions/files/src/main/java/com/example/HelloAIFunction.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,31 @@ public String handleRequest(String input) {
101101

102102
} catch (Exception e) {
103103
try {
104-
AuthenticationDetailsProvider authenticationDetailsProvider =
105-
SimpleAuthenticationDetailsProvider.builder()
106-
.tenantId(TENANCY_ID)
107-
.userId(USER_ID)
108-
.fingerprint(FINGERPRINT)
109-
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
110-
.passPhrase(PASSPHRASE)
111-
.build();
104+
ConfigFileAuthenticationDetailsProvider configFileAuthenticationDetailsProvider =
105+
new ConfigFileAuthenticationDetailsProvider("/config", "DEFAULT");
112106
generativeAiInferenceClient =
113107
GenerativeAiInferenceClient.builder()
114108
.region(REGION)
115109
.endpoint(ENDPOINT)
116-
.build(authenticationDetailsProvider);
110+
.build(configFileAuthenticationDetailsProvider);
117111
} catch (Exception ee) {
118-
answer = answer + "\n" + ee.getMessage();
112+
try {
113+
AuthenticationDetailsProvider authenticationDetailsProvider =
114+
SimpleAuthenticationDetailsProvider.builder()
115+
.tenantId(TENANCY_ID)
116+
.userId(USER_ID)
117+
.fingerprint(FINGERPRINT)
118+
.privateKeySupplier(new StringPrivateKeySupplier(PRIVATEKEY))
119+
.passPhrase(PASSPHRASE)
120+
.build();
121+
generativeAiInferenceClient =
122+
GenerativeAiInferenceClient.builder()
123+
.region(REGION)
124+
.endpoint(ENDPOINT)
125+
.build(authenticationDetailsProvider);
126+
} catch (Exception eee) {
127+
answer = answer + "\n" + eee.getMessage();
128+
}
119129
}
120130
}
121131

@@ -156,5 +166,4 @@ public String handleRequest(String input) {
156166
}
157167
return answer;
158168
}
159-
160169
}

0 commit comments

Comments
 (0)