From 439d9d52b10f4606007c7adbd3fe1805d1888bee Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Tue, 3 Aug 2021 13:13:13 +0530 Subject: [PATCH 1/9] Onsr functions examples with certs --- sample_onsr/.DS_Store | Bin 0 -> 6148 bytes .../func.go | 105 +++++++++++++++++ .../func.yaml | 7 ++ .../go.mod | 8 ++ .../func.yaml | 8 ++ .../pom.xml | 82 +++++++++++++ .../fn/ObjectStorageOnsrPutObject.java | 110 ++++++++++++++++++ .../func.py | 51 ++++++++ .../func.yaml | 8 ++ .../requirements.txt | 2 + .../Gemfile | 4 + .../func.rb | 36 ++++++ .../func.yaml | 9 ++ 13 files changed, 430 insertions(+) create mode 100644 sample_onsr/.DS_Store create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-go/func.go create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-go/func.yaml create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-go/go.mod create mode 100755 sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-java/pom.xml create mode 100755 sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-python/func.py create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-python/requirements.txt create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-ruby/Gemfile create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb create mode 100644 sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.yaml diff --git a/sample_onsr/.DS_Store b/sample_onsr/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..19e1fed3aa63d3de6f6b986614da44492d95462f GIT binary patch literal 6148 zcmeHKu};G<5IsYsMzC~bsg!|%rHIZfp$Y?Y#R3bYMJ=es5v2oL{)3gBfluI5Soi^E zc;`FOL@is25W17j&#`@H`JL^HiOBWl!!A*ah$1LsV++L!;eOU7+0YiYbHRtFjBe

IM+U3HQvp~b*!lz_tR$k@JZrc`&y=c5&w10vtl3c zUMLH4HcPZEXr(b=3>X974DkIBLm3mrLNHt%D8dr}*g$g-)H(IDO{|zG7J}%3I57o^ zsnaKh6LYl3IxbNx1jU@3K0chz?DU1=!tC%LhvDQBK`V^`W1z~wrhK+I{~!PS{;wL@ zmoZ=r{3`}rBkiX>EJ<_g(&9MR#?T8W3y&)Vg9!>v$DzWhcn2B;eKA*niDDs$7Kr@_ M1RAU`27Z-+F90UrZ~y=R literal 0 HcmV?d00001 diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go new file mode 100644 index 0000000..c5d97cb --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -0,0 +1,105 @@ +/* + oci-objectstorage-onsr-put-object-go version 1.0. + + Copyright (c) 2021 Oracle, Inc. All rights reserved. + Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. +*/ + +package main + +import ( + "context" + //"crypto/tls" + //"crypto/x509" + "encoding/json" + "fmt" + "io" + //"io/ioutil" + "os" + "path" + + fdk "github.com/fnproject/fdk-go" + "github.com/oracle/oci-go-sdk/v37/common" + "github.com/oracle/oci-go-sdk/v37/common/auth" + "github.com/oracle/oci-go-sdk/v37/example/helpers" + "github.com/oracle/oci-go-sdk/v37/objectstorage" +) + +type ObjectStorage_Bucket struct { + Name string `json:"bucket"` +} + +func main() { + fdk.Handle(fdk.HandlerFunc(myHandler)) +} + +func putObject(ctx context.Context, c objectstorage.ObjectStorageClient, namespace, bucketname, objectname string, contentLen int64, content io.ReadCloser, metadata map[string]string) error { + request := objectstorage.PutObjectRequest{ + NamespaceName: common.String(namespace), + BucketName: common.String(bucketname), + ObjectName: common.String(objectname), + ContentLength: common.Int64(contentLen), + PutObjectBody: content, + OpcMeta: metadata, + } + _, err := c.PutObject(ctx, request) + fmt.Println("put object") + return err +} + +func ObjectStorage_UploadFile(ctx context.Context, bname string) { + // Get auth + provider, err := auth.ResourcePrincipalConfigurationProvider() + helpers.FatalIfError(err) + + c, clerr := objectstorage.NewObjectStorageClientWithConfigurationProvider(provider) + helpers.FatalIfError(clerr) + + /* + // Certs are mounted at this location for ONSR realms + cert_file := "/python/certifi/cacert.pem" + cert, err := ioutil.ReadFile(cert_file) + helpers.FatalIfError(err) + + // Adding extra certs + pool := x509.NewCertPool() + pool.AppendCertsFromPEM([]byte(string(cert))) + + //install the certificates to the client + if h, ok := c.HTTPClient.(*http.Client); ok { + tr := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool}} + h.Transport = tr + } else { + panic("the client dispatcher is not of http.Client type. can not patch the tls config") + } + */ + + request := objectstorage.GetNamespaceRequest{} + r, err := c.GetNamespace(ctx, request) + helpers.FatalIfError(err) + + namespace := *r.Value + + contentlen := 1024 * 1000 + filepath, filesize := helpers.WriteTempFileOfSize(int64(contentlen)) + filename := path.Base(filepath) + + file, e := os.Open(filepath) + defer file.Close() + helpers.FatalIfError(e) + + e = putObject(ctx, c, namespace, bname, filename, filesize, file, nil) + helpers.FatalIfError(e) +} + +func myHandler(ctx context.Context, in io.Reader, out io.Writer) { + bucket := &ObjectStorage_Bucket{Name: "bucket"} + json.NewDecoder(in).Decode(bucket) + ObjectStorage_UploadFile(ctx, bucket.Name) + msg := struct { + Msg string `json:"message"` + }{ + Msg: "Request Completed", + } + json.NewEncoder(out).Encode(&msg) +} diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.yaml b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.yaml new file mode 100644 index 0000000..e39f10e --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.yaml @@ -0,0 +1,7 @@ +schema_version: 20180708 +name: oci-objectstorage-onsr-put-object-go +version: 0.0.1 +runtime: go +build_image: fnproject/go:1.15-dev +run_image: fnproject/go:1.15 +entrypoint: ./func diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/go.mod b/sample_onsr/oci-objectstorage-onsr-put-object-go/go.mod new file mode 100644 index 0000000..db70e0c --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/go.mod @@ -0,0 +1,8 @@ +module func + +go 1.16 + +require ( + github.com/fnproject/fdk-go v0.0.6 + github.com/oracle/oci-go-sdk/v37 v37.0.0 +) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml b/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml new file mode 100755 index 0000000..6d1553d --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml @@ -0,0 +1,8 @@ +schema_version: 20180708 +name: oci-objectstorage-onsr-put-object-java +version: 0.0.17 +runtime: java +build_image: fnproject/fn-java-fdk-build:jdk11-1.0.130 +run_image: fnproject/fn-java-fdk:jre11-1.0.130 +cmd: com.example.fn.ObjectStorageOnsrPutObject::handle +timeout: 120 diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-java/pom.xml b/sample_onsr/oci-objectstorage-onsr-put-object-java/pom.xml new file mode 100644 index 0000000..f9d753f --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + UTF-8 + 1.0.130 + + com.example.fn + oci-objectstorage-put-object + 1.0.0 + + + + + com.oracle.oci.sdk + oci-java-sdk-bom + 1.36.2 + pom + import + + + + + + + com.fnproject.fn + api + ${fdk.version} + + + com.fnproject.fn + testing-core + ${fdk.version} + test + + + com.fnproject.fn + testing-junit4 + ${fdk.version} + test + + + junit + junit + 4.13.2 + test + + + com.oracle.oci.sdk + oci-java-sdk-objectstorage + + + com.sun.activation + jakarta.activation + 1.2.1 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 11 + 11 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + false + + + + + diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java b/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java new file mode 100755 index 0000000..387dd47 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java @@ -0,0 +1,110 @@ +/* +** ObjectStorageOnsrPutObject version 1.0. +** +** Copyright (c) 2020 Oracle, Inc. +** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. +*/ + +package com.example.fn; + +import com.oracle.bmc.auth.ResourcePrincipalAuthenticationDetailsProvider; +import com.oracle.bmc.objectstorage.ObjectStorage; +import com.oracle.bmc.objectstorage.ObjectStorageClient; +import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest; +import com.oracle.bmc.objectstorage.requests.PutObjectRequest; +import com.oracle.bmc.objectstorage.responses.PutObjectResponse; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; + +public class ObjectStorageOnsrPutObject { + + private ObjectStorage objStoreClient = null; + final ResourcePrincipalAuthenticationDetailsProvider provider + = ResourcePrincipalAuthenticationDetailsProvider.builder().build(); + + public ObjectStorageOnsrPutObject() { + try { + //print env vars in Functions container + System.err.println("OCI_RESOURCE_PRINCIPAL_VERSION " + System.getenv("OCI_RESOURCE_PRINCIPAL_VERSION")); + System.err.println("OCI_RESOURCE_PRINCIPAL_REGION " + System.getenv("OCI_RESOURCE_PRINCIPAL_REGION")); + System.err.println("OCI_RESOURCE_PRINCIPAL_RPST " + System.getenv("OCI_RESOURCE_PRINCIPAL_RPST")); + System.err.println("OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM " + System.getenv("OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM")); + + // Adding certificates to trust store + //Path cacertFile = Files.createTempFile("cacerts", null); + //Files.write(cacertFile, request.trustStore); + //System.setProperty("javax.net.ssl.trustStore", cacertFile.toString()); + objStoreClient = new ObjectStorageClient(provider); + + } catch (Throwable ex) { + System.err.println("Failed to instantiate ObjectStorage client - " + ex.getMessage()); + } + } + + public static class ObjectInfo { + + private String name; + private String bucketName; + private String content; + + public String getBucketName() { + return bucketName; + } + + public void setBucketName(String bucketName) { + this.bucketName = bucketName; + } + + public ObjectInfo() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + } + + public String handle(ObjectInfo objectInfo) { + String result = "FAILED"; + + if (objStoreClient == null) { + System.err.println("There was a problem creating the ObjectStorage Client object. Please check logs"); + return result; + } + try { + GetNamespaceRequest request = GetNamespaceRequest.builder().build(); + String nameSpace = objStoreClient.getNamespace(request).getValue(); + String name = "onsr_cert_test"; + + PutObjectRequest por = PutObjectRequest.builder() + .namespaceName(nameSpace) + .bucketName(objectInfo.bucketName) + .objectName(name) + .putObjectBody(new ByteArrayInputStream(objectInfo.content.getBytes(StandardCharsets.UTF_8))) + .build(); + + PutObjectResponse poResp = objStoreClient.putObject(por); + result = "Successfully submitted Put request for object " + name + " in bucket " + objectInfo.bucketName + ". OPC reuquest ID is " + poResp.getOpcRequestId(); + System.err.println(result); + + } catch (Throwable e) { + System.err.println("Error storing object in bucket " + e.getMessage()); + result = "Error storing object in bucket " + e.getMessage(); + } + + return result; + } +} diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py new file mode 100644 index 0000000..bd730f5 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py @@ -0,0 +1,51 @@ +# +# oci-objectstorage-onsr-put-object-python version 1.0. +# +# Copyright (c) 2020 Oracle, Inc. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. +# + +import io +import os +import json +import sys +from fdk import response + +import oci.object_storage + +# Certs are mounted at this location for ONSR realms +cert_file = "/python/certifi/cacert.pem" +# file to upload +file_to_upload = "onsr_cert_test" +file_to_upload_content = {"content":"This is test file for ONSR test"} + +def handler(ctx, data: io.BytesIO=None): + try: + body = json.loads(data.getvalue()) + bucketName = body["bucketName"] + except Exception: + error = """ + Input a JSON object in the format: '{"bucketName": "", + "content": "", "objectName": ""}' + """ + raise Exception(error) + signer = oci.auth.signers.get_resource_principals_signer() + client = oci.object_storage.ObjectStorageClient(config={}, signer=signer) + #client.base_client.session.cert = cert_file + + resp = put_object(client, bucketName, file_to_upload, file_to_upload_content) + return response.Response( + ctx, + response_data=json.dumps(resp), + headers={"Content-Type": "application/json"} + ) + +def put_object(client, bucketName, objectName, content): + namespace = client.get_namespace().data + output="" + try: + object = client.put_object(namespace, bucketName, objectName, json.dumps(content)) + output = "Success: Put object '" + objectName + "' in bucket '" + bucketName + "'" + except Exception as e: + output = "Failed: " + str(e.message) + return { "state": output } \ No newline at end of file diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml new file mode 100644 index 0000000..af4f448 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml @@ -0,0 +1,8 @@ +schema_version: 20180708 +name: oci-objectstorage-onsr-put-object-python +version: 0.0.3 +runtime: python +build_image: fnproject/python:3.8-dev +run_image: fnproject/python:3.8 +entrypoint: /python/bin/fdk /function/func.py handler +memory: 256 diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/requirements.txt b/sample_onsr/oci-objectstorage-onsr-put-object-python/requirements.txt new file mode 100644 index 0000000..684bfd2 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/requirements.txt @@ -0,0 +1,2 @@ +fdk>=0.1.34 +oci>=2.0.0 \ No newline at end of file diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/Gemfile b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/Gemfile new file mode 100644 index 0000000..495fbf1 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/Gemfile @@ -0,0 +1,4 @@ +source 'https://www.rubygems.org' do + gem 'fdk', '>= 0.0.24' + gem 'oci', '>= 2.0' +end diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb new file mode 100644 index 0000000..8c484ef --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb @@ -0,0 +1,36 @@ +# +# oci-objectstorage-onsr-put-object-ruby version 1.0. +# +# Copyright (c) 2021 Oracle, Inc. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. +# + +require 'fdk' + +# Certs are mounted at this location for ONSR realms +$cert_file = '/etc/ssl/cert.pem' +# file to upload +$file_to_upload = "onsr_cert_test" +$file_to_upload_content = "This is test file for ONSR test" + +def put_object(bucket) + FDK.log(entry: "put_object: enter") + require 'oci' + principal_signer = OCI::Auth::Signers.resource_principals_signer + object_storage_client = OCI::ObjectStorage::ObjectStorageClient.new(signer: principal_signer) + namespace = object_storage_client.get_namespace.data + + object_storage_client.api_client.request_option_overrides = { + ca_file: $cert_file + } + get_object_response = object_storage_client.put_object(namespace, bucket, $file_to_upload, $file_to_upload_content) + FDK.log(entry: "put_object: exit") +end + +def myfunction(context:, input:) + bucket = input.respond_to?(:fetch) ? input.fetch('bucket') : input + put_object(bucket) + { message: "Completed Successfully!!!" } +end + +FDK.handle(target: :myfunction) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.yaml b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.yaml new file mode 100644 index 0000000..20fb8b7 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.yaml @@ -0,0 +1,9 @@ +schema_version: 20180708 +name: oci-objectstorage-onsr-put-object-ruby +version: 0.0.3 +runtime: ruby +build_image: fnproject/ruby:2.7-dev +run_image: fnproject/ruby:2.7 +entrypoint: ruby func.rb +memory: 1024 +timeout: 300 From 1115f7deb4a4994445c64f1da1f881a501c911c7 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Tue, 3 Aug 2021 13:17:27 +0530 Subject: [PATCH 2/9] removing .DStore --- sample_onsr/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 sample_onsr/.DS_Store diff --git a/sample_onsr/.DS_Store b/sample_onsr/.DS_Store deleted file mode 100644 index 19e1fed3aa63d3de6f6b986614da44492d95462f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKu};G<5IsYsMzC~bsg!|%rHIZfp$Y?Y#R3bYMJ=es5v2oL{)3gBfluI5Soi^E zc;`FOL@is25W17j&#`@H`JL^HiOBWl!!A*ah$1LsV++L!;eOU7+0YiYbHRtFjBe

IM+U3HQvp~b*!lz_tR$k@JZrc`&y=c5&w10vtl3c zUMLH4HcPZEXr(b=3>X974DkIBLm3mrLNHt%D8dr}*g$g-)H(IDO{|zG7J}%3I57o^ zsnaKh6LYl3IxbNx1jU@3K0chz?DU1=!tC%LhvDQBK`V^`W1z~wrhK+I{~!PS{;wL@ zmoZ=r{3`}rBkiX>EJ<_g(&9MR#?T8W3y&)Vg9!>v$DzWhcn2B;eKA*niDDs$7Kr@_ M1RAU`27Z-+F90UrZ~y=R From 6437301fdc64bdb371d8082157957ce4b9ce2429 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Mon, 9 Aug 2021 10:55:54 +0530 Subject: [PATCH 3/9] Chan ges based on Review --- sample_onsr/oci-objectstorage-onsr-put-object-go/func.go | 4 ++-- sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml | 1 - sample_onsr/oci-objectstorage-onsr-put-object-python/func.py | 4 ++-- sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go index c5d97cb..38fa045 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -57,8 +57,8 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { /* // Certs are mounted at this location for ONSR realms - cert_file := "/python/certifi/cacert.pem" - cert, err := ioutil.ReadFile(cert_file) + cert_file_path := "/python/certifi/cacert.pem" + cert, err := ioutil.ReadFile(cert_file_path) helpers.FatalIfError(err) // Adding extra certs diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml b/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml index 6d1553d..b09b4ef 100755 --- a/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml @@ -5,4 +5,3 @@ runtime: java build_image: fnproject/fn-java-fdk-build:jdk11-1.0.130 run_image: fnproject/fn-java-fdk:jre11-1.0.130 cmd: com.example.fn.ObjectStorageOnsrPutObject::handle -timeout: 120 diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py index bd730f5..cfc8aca 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py @@ -14,7 +14,7 @@ import oci.object_storage # Certs are mounted at this location for ONSR realms -cert_file = "/python/certifi/cacert.pem" +cert_file_path = "/python/certifi/cacert.pem" # file to upload file_to_upload = "onsr_cert_test" file_to_upload_content = {"content":"This is test file for ONSR test"} @@ -31,7 +31,7 @@ def handler(ctx, data: io.BytesIO=None): raise Exception(error) signer = oci.auth.signers.get_resource_principals_signer() client = oci.object_storage.ObjectStorageClient(config={}, signer=signer) - #client.base_client.session.cert = cert_file + #client.base_client.session.cert = cert_file_path resp = put_object(client, bucketName, file_to_upload, file_to_upload_content) return response.Response( diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb index 8c484ef..46f1922 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb +++ b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb @@ -8,7 +8,7 @@ require 'fdk' # Certs are mounted at this location for ONSR realms -$cert_file = '/etc/ssl/cert.pem' +$cert_file_path = '/etc/ssl/cert.pem' # file to upload $file_to_upload = "onsr_cert_test" $file_to_upload_content = "This is test file for ONSR test" @@ -21,7 +21,7 @@ def put_object(bucket) namespace = object_storage_client.get_namespace.data object_storage_client.api_client.request_option_overrides = { - ca_file: $cert_file + ca_file: $cert_file_path } get_object_response = object_storage_client.put_object(namespace, bucket, $file_to_upload, $file_to_upload_content) FDK.log(entry: "put_object: exit") From fd7fd30e5826be043a6d392359075dd04e7e7581 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Mon, 9 Aug 2021 11:03:01 +0530 Subject: [PATCH 4/9] Correct names of variables --- .../oci-objectstorage-onsr-put-object-go/func.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go index 38fa045..ae1d2ad 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -52,8 +52,8 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { provider, err := auth.ResourcePrincipalConfigurationProvider() helpers.FatalIfError(err) - c, clerr := objectstorage.NewObjectStorageClientWithConfigurationProvider(provider) - helpers.FatalIfError(clerr) + client, client_err := objectstorage.NewObjectStorageClientWithConfigurationProvider(provider) + helpers.FatalIfError(client_err) /* // Certs are mounted at this location for ONSR realms @@ -66,7 +66,7 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { pool.AppendCertsFromPEM([]byte(string(cert))) //install the certificates to the client - if h, ok := c.HTTPClient.(*http.Client); ok { + if h, ok := client.HTTPClient.(*http.Client); ok { tr := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool}} h.Transport = tr } else { @@ -75,7 +75,7 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { */ request := objectstorage.GetNamespaceRequest{} - r, err := c.GetNamespace(ctx, request) + r, err := client.GetNamespace(ctx, request) helpers.FatalIfError(err) namespace := *r.Value @@ -88,7 +88,7 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { defer file.Close() helpers.FatalIfError(e) - e = putObject(ctx, c, namespace, bname, filename, filesize, file, nil) + e = putObject(ctx, client, namespace, bname, filename, filesize, file, nil) helpers.FatalIfError(e) } From 0b784b8b9bafa8866b9c0894b768c5594422a894 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Mon, 9 Aug 2021 13:30:31 +0530 Subject: [PATCH 5/9] adding message in golang program --- sample_onsr/oci-objectstorage-onsr-put-object-go/func.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go index ae1d2ad..84efe39 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -99,7 +99,7 @@ func myHandler(ctx context.Context, in io.Reader, out io.Writer) { msg := struct { Msg string `json:"message"` }{ - Msg: "Request Completed", + Msg: filename + " uploaded successfully in bucket " + bucket.Name, } json.NewEncoder(out).Encode(&msg) } From 049db68c9f06ce4e4f17d591386ecb0b41a673e8 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Mon, 9 Aug 2021 13:32:20 +0530 Subject: [PATCH 6/9] remove un necessary print --- sample_onsr/oci-objectstorage-onsr-put-object-go/func.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go index 84efe39..3a21d8f 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -12,7 +12,6 @@ import ( //"crypto/tls" //"crypto/x509" "encoding/json" - "fmt" "io" //"io/ioutil" "os" @@ -43,7 +42,6 @@ func putObject(ctx context.Context, c objectstorage.ObjectStorageClient, namespa OpcMeta: metadata, } _, err := c.PutObject(ctx, request) - fmt.Println("put object") return err } From c35d790905219ba2cc7e1fb2c60bfaed3ab6b5f8 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Tue, 10 Aug 2021 10:14:35 +0530 Subject: [PATCH 7/9] Update certificate file path --- .../func.go | 24 ++++++++++++------- .../fn/ObjectStorageOnsrPutObject.java | 9 +++---- .../func.py | 5 ++-- .../func.rb | 10 ++++---- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go index 3a21d8f..a1d2414 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -9,11 +9,11 @@ package main import ( "context" - //"crypto/tls" - //"crypto/x509" + "crypto/tls" + "crypto/x509" "encoding/json" "io" - //"io/ioutil" + "io/ioutil" "os" "path" @@ -45,6 +45,15 @@ func putObject(ctx context.Context, c objectstorage.ObjectStorageClient, namespa return err } +func fileExists(filename string) bool { + // fileExists checks if a file exists + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + func ObjectStorage_UploadFile(ctx context.Context, bname string) { // Get auth provider, err := auth.ResourcePrincipalConfigurationProvider() @@ -53,9 +62,9 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { client, client_err := objectstorage.NewObjectStorageClientWithConfigurationProvider(provider) helpers.FatalIfError(client_err) - /* - // Certs are mounted at this location for ONSR realms - cert_file_path := "/python/certifi/cacert.pem" + // Certs are mounted at this location for ONSR realms + cert_file_path := "/etc/oci-pki/customer/customer-cert.pem" + if fileExists(cert_file_path) { cert, err := ioutil.ReadFile(cert_file_path) helpers.FatalIfError(err) @@ -70,8 +79,7 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { } else { panic("the client dispatcher is not of http.Client type. can not patch the tls config") } - */ - + } request := objectstorage.GetNamespaceRequest{} r, err := client.GetNamespace(ctx, request) helpers.FatalIfError(err) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java b/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java index 387dd47..83ebe07 100755 --- a/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java @@ -16,6 +16,7 @@ import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; +import java.io.File; public class ObjectStorageOnsrPutObject { @@ -32,11 +33,11 @@ public ObjectStorageOnsrPutObject() { System.err.println("OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM " + System.getenv("OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM")); // Adding certificates to trust store - //Path cacertFile = Files.createTempFile("cacerts", null); - //Files.write(cacertFile, request.trustStore); - //System.setProperty("javax.net.ssl.trustStore", cacertFile.toString()); + File cacertFile = new File("/etc/oci-pki/customer/customer-cert.pem"); + if (cacertFile.exists()) { + System.setProperty("javax.net.ssl.trustStore", cacertFile.getAbsolutePath()); + } objStoreClient = new ObjectStorageClient(provider); - } catch (Throwable ex) { System.err.println("Failed to instantiate ObjectStorage client - " + ex.getMessage()); } diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py index cfc8aca..db9b2cc 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py @@ -14,7 +14,7 @@ import oci.object_storage # Certs are mounted at this location for ONSR realms -cert_file_path = "/python/certifi/cacert.pem" +cert_file_path = "/etc/oci-pki/customer/customer-cert.pem" # file to upload file_to_upload = "onsr_cert_test" file_to_upload_content = {"content":"This is test file for ONSR test"} @@ -31,7 +31,8 @@ def handler(ctx, data: io.BytesIO=None): raise Exception(error) signer = oci.auth.signers.get_resource_principals_signer() client = oci.object_storage.ObjectStorageClient(config={}, signer=signer) - #client.base_client.session.cert = cert_file_path + if os.path.exists(cert_file_path): + client.base_client.session.cert = cert_file_path resp = put_object(client, bucketName, file_to_upload, file_to_upload_content) return response.Response( diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb index 46f1922..662bf14 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb +++ b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb @@ -8,7 +8,7 @@ require 'fdk' # Certs are mounted at this location for ONSR realms -$cert_file_path = '/etc/ssl/cert.pem' +$cert_file_path = '/etc/oci-pki/customer/customer-cert.pem' # file to upload $file_to_upload = "onsr_cert_test" $file_to_upload_content = "This is test file for ONSR test" @@ -20,9 +20,11 @@ def put_object(bucket) object_storage_client = OCI::ObjectStorage::ObjectStorageClient.new(signer: principal_signer) namespace = object_storage_client.get_namespace.data - object_storage_client.api_client.request_option_overrides = { - ca_file: $cert_file_path - } + if(File.exist?($cert_file_path)) + object_storage_client.api_client.request_option_overrides = { + ca_file: $cert_file_path + } + end get_object_response = object_storage_client.put_object(namespace, bucket, $file_to_upload, $file_to_upload_content) FDK.log(entry: "put_object: exit") end From 157c8a857511b19da3e72653c5a759b62461e386 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Thu, 12 Aug 2021 10:17:25 +0530 Subject: [PATCH 8/9] python fix --- sample_onsr/oci-objectstorage-onsr-put-object-python/func.py | 2 +- sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py index db9b2cc..068cd00 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py @@ -32,7 +32,7 @@ def handler(ctx, data: io.BytesIO=None): signer = oci.auth.signers.get_resource_principals_signer() client = oci.object_storage.ObjectStorageClient(config={}, signer=signer) if os.path.exists(cert_file_path): - client.base_client.session.cert = cert_file_path + client.base_client.session.verify = cert_file_path resp = put_object(client, bucketName, file_to_upload, file_to_upload_content) return response.Response( diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml index af4f448..e26930a 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml @@ -1,6 +1,6 @@ schema_version: 20180708 name: oci-objectstorage-onsr-put-object-python -version: 0.0.3 +version: 0.0.10 runtime: python build_image: fnproject/python:3.8-dev run_image: fnproject/python:3.8 From bed211bf69d4a3741081725a5478c7729e9ce7a2 Mon Sep 17 00:00:00 2001 From: Mohit Mangal Date: Fri, 13 Aug 2021 10:19:39 +0530 Subject: [PATCH 9/9] import http --- sample_onsr/oci-objectstorage-onsr-put-object-go/func.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go index a1d2414..d51ed29 100644 --- a/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -14,6 +14,7 @@ import ( "encoding/json" "io" "io/ioutil" + "net/http" "os" "path" @@ -54,7 +55,7 @@ func fileExists(filename string) bool { return !info.IsDir() } -func ObjectStorage_UploadFile(ctx context.Context, bname string) { +func ObjectStorage_UploadFile(ctx context.Context, bname string) string { // Get auth provider, err := auth.ResourcePrincipalConfigurationProvider() helpers.FatalIfError(err) @@ -96,12 +97,13 @@ func ObjectStorage_UploadFile(ctx context.Context, bname string) { e = putObject(ctx, client, namespace, bname, filename, filesize, file, nil) helpers.FatalIfError(e) + return filename } func myHandler(ctx context.Context, in io.Reader, out io.Writer) { bucket := &ObjectStorage_Bucket{Name: "bucket"} json.NewDecoder(in).Decode(bucket) - ObjectStorage_UploadFile(ctx, bucket.Name) + filename := ObjectStorage_UploadFile(ctx, bucket.Name) msg := struct { Msg string `json:"message"` }{