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..d51ed29 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-go/func.go @@ -0,0 +1,113 @@ +/* + 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" + "io" + "io/ioutil" + "net/http" + "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) + 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) string { + // Get auth + provider, err := auth.ResourcePrincipalConfigurationProvider() + helpers.FatalIfError(err) + + client, client_err := objectstorage.NewObjectStorageClientWithConfigurationProvider(provider) + helpers.FatalIfError(client_err) + + // 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) + + // Adding extra certs + pool := x509.NewCertPool() + pool.AppendCertsFromPEM([]byte(string(cert))) + + //install the certificates to the client + if h, ok := client.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 := client.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, 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) + filename := ObjectStorage_UploadFile(ctx, bucket.Name) + msg := struct { + Msg string `json:"message"` + }{ + Msg: filename + " uploaded successfully in bucket " + bucket.Name, + } + 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..b09b4ef --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/func.yaml @@ -0,0 +1,7 @@ +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 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..83ebe07 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java @@ -0,0 +1,111 @@ +/* +** 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; +import java.io.File; + +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 + 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()); + } + } + + 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..068cd00 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-python/func.py @@ -0,0 +1,52 @@ +# +# 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_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"} + +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) + if os.path.exists(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( + 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..e26930a --- /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.10 +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..662bf14 --- /dev/null +++ b/sample_onsr/oci-objectstorage-onsr-put-object-ruby/func.rb @@ -0,0 +1,38 @@ +# +# 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_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" + +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 + + 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 + +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