-
Notifications
You must be signed in to change notification settings - Fork 85
Onsr functions examples with certs #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mohitmagal
wants to merge
9
commits into
master
Choose a base branch
from
onsr_samples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
439d9d5
Onsr functions examples with certs
mohitmagal 1115f7d
removing .DStore
mohitmagal 6437301
Chan ges based on Review
mohitmagal fd7fd30
Correct names of variables
mohitmagal 0b784b8
adding message in golang program
mohitmagal 049db68
remove un necessary print
mohitmagal c35d790
Update certificate file path
mohitmagal 157c8a8
python fix
mohitmagal bed211b
import http
mohitmagal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
sample_onsr/oci-objectstorage-onsr-put-object-go/func.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} | ||
mohitmagal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
82 changes: 82 additions & 0 deletions
82
sample_onsr/oci-objectstorage-onsr-put-object-java/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<fdk.version>1.0.130</fdk.version> | ||
</properties> | ||
<groupId>com.example.fn</groupId> | ||
<artifactId>oci-objectstorage-put-object</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.oracle.oci.sdk</groupId> | ||
<artifactId>oci-java-sdk-bom</artifactId> | ||
<version>1.36.2</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.fnproject.fn</groupId> | ||
<artifactId>api</artifactId> | ||
<version>${fdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fnproject.fn</groupId> | ||
<artifactId>testing-core</artifactId> | ||
<version>${fdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fnproject.fn</groupId> | ||
<artifactId>testing-junit4</artifactId> | ||
<version>${fdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.oci.sdk</groupId> | ||
<artifactId>oci-java-sdk-objectstorage</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.activation</groupId> | ||
<artifactId>jakarta.activation</artifactId> | ||
<version>1.2.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.3</version> | ||
<configuration> | ||
<source>11</source> | ||
<target>11</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
<configuration> | ||
<useSystemClassLoader>false</useSystemClassLoader> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
111 changes: 111 additions & 0 deletions
111
...storage-onsr-put-object-java/src/main/java/com/example/fn/ObjectStorageOnsrPutObject.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
sample_onsr/oci-objectstorage-onsr-put-object-python/func.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "<bucket name>", | ||
"content": "<content>", "objectName": "<object name>"}' | ||
""" | ||
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 } |
8 changes: 8 additions & 0 deletions
8
sample_onsr/oci-objectstorage-onsr-put-object-python/func.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.