Skip to content
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

***Do Not Merge*** #1818

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*.dll
*.so
*.dylib

!libsdk.so
# Test binary, built with `go test -c`
*.test

Expand Down
24 changes: 24 additions & 0 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@
}, nil
}

//#region ConfigWrapper
type ConfigWrapper struct {
internal *config
}
func NewConfigWrapper() *ConfigWrapper {
return &ConfigWrapper{
internal: &config{},
}
}

func (cw *ConfigWrapper) SetPlatformConfiguration(pc PlatformConfiguration) {
cw.internal.PlatformConfiguration = pc
}

func (cw *ConfigWrapper) SetDialOption(dialOption grpc.DialOption) {
cw.internal.dialOption = dialOption
}

func (cw *ConfigWrapper) InternalConfig() *config {

Check failure on line 216 in sdk/sdk.go

View workflow job for this annotation

GitHub Actions / go (sdk)

unexported-return: exported method InternalConfig returns unexported type *sdk.config, which can be annoying to use (revive)
return cw.internal
}

//#endregion

func SanitizePlatformEndpoint(e string) (string, error) {
// check if there's a scheme, if not, add https
if !regexp.MustCompile(`^https?://`).MatchString(e) {
Expand Down
89 changes: 89 additions & 0 deletions service/libsdk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package github.com/opentdf/platform/service */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h>

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments. */


#line 3 "sdk_wrapper.go"

#include <stdlib.h>

#line 1 "cgo-generated-wrapper"


/* End of preamble from import "C" comments. */


/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef size_t GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
#ifdef _MSC_VER
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif

/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus
extern "C" {
#endif

extern GoUintptr CreateConfig();
extern char* SetClientCredentials(GoUintptr configID, char* clientID, char* clientSecret);
extern char* SetInsecureSkipVerify(GoUintptr configID);
extern GoUintptr InitializeSdk(GoUintptr configID, char* platformEndpoint, char** errMessage);

#ifdef __cplusplus
}
#endif
Binary file added service/libsdk.so
Binary file not shown.
94 changes: 94 additions & 0 deletions service/sdk_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

/*
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"unsafe"

"github.com/opentdf/platform/sdk"
)

var SdkInstance *sdk.SDK

// Global config store
var (
configStore = map[uintptr]*sdk.ConfigWrapper{}
clientCredentialsMap = map[uintptr]struct {
ClientID string
ClientSecret string
}{}
currentConfigID uintptr = 1
)

//export CreateConfig
func CreateConfig() uintptr {
config := sdk.NewConfigWrapper()
configID := currentConfigID
configStore[configID] = config
currentConfigID++
return configID
}

//export SetClientCredentials
func SetClientCredentials(configID uintptr, clientID *C.char, clientSecret *C.char) *C.char {
if clientID == nil || clientSecret == nil {
return C.CString("Client ID and Client Secret are required")
}

_, ok := configStore[configID]
if !ok {
return C.CString("Invalid config ID")
}

clientCredentialsMap[configID] = struct {
ClientID string
ClientSecret string
}{
ClientID: C.GoString(clientID),
ClientSecret: C.GoString(clientSecret),
}

return nil
}

//export SetInsecureSkipVerify
func SetInsecureSkipVerify(configID uintptr) *C.char {
config, ok := configStore[configID]
if !ok {
return C.CString("Invalid config ID")
}

option := sdk.WithInsecureSkipVerifyConn()
if option == nil {
return C.CString("Failed to apply insecure skip verify option")
}

option(config.InternalConfig())
return nil
}

//export InitializeSdk
func InitializeSdk(configID uintptr, platformEndpoint *C.char, errMessage **C.char) uintptr {
// Convert C string to Go string
goEndpoint := C.GoString(platformEndpoint)

var opts []sdk.Option

if creds, exists := clientCredentialsMap[configID]; exists {
opts = append(opts, sdk.WithClientCredentials(creds.ClientID, creds.ClientSecret, []string{}))
}

var err error
sdkInstance, err := sdk.New(goEndpoint, opts...)
if err != nil {
// Set the error message and return nil pointer
*errMessage = C.CString(fmt.Sprintf("Error initializing SDK: %s", err.Error()))
return 0
}

// Return a success message
return uintptr(unsafe.Pointer(sdkInstance))
}
Loading