Skip to content

Commit 24accc8

Browse files
committed
Add correct file path and correct running version
1 parent 3572208 commit 24accc8

File tree

6 files changed

+115
-53
lines changed

6 files changed

+115
-53
lines changed

client_builder.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error) {
2626
}
2727
}
2828

29+
if client.usesDesktopApp && client.config.SAToken != "" {
30+
return nil, fmt.Errorf("cannot use both SA token and desktop app authentication")
31+
}
32+
2933
var core internal.Core
3034
var err error
3135
if client.usesDesktopApp {
32-
core, err = internal.NewSharedLibCore()
36+
core, err = internal.GetSharedLibCore()
3337
} else {
34-
core, err = internal.GetSharedCore()
38+
core, err = internal.GetExtismCore()
3539
}
3640

3741
if err != nil {

example/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
// [developer-docs.sdk.go.sdk-import]-start
1414
import "github.com/1password/onepassword-sdk-go"
15+
1516
// [developer-docs.sdk.go.sdk-import]-end
1617

1718
func main() {

internal/shared_extism_core.go renamed to internal/extism_core.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ const (
2424
releaseClientFuncName = "release_client"
2525
)
2626

27-
var core *SharedCore
27+
var core *ExtismCore
2828

29-
// GetSharedCore initializes the shared core once and returns the already existing one on subsequent calls.
30-
func GetSharedCore() (*SharedCore, error) {
29+
// GetExtismCore initializes the shared core once and returns the already existing one on subsequent calls.
30+
func GetExtismCore() (*ExtismCore, error) {
3131
runtimeCtx := context.Background()
3232
if core == nil {
3333
p, err := loadWASM(runtimeCtx)
3434
if err != nil {
3535
return nil, err
3636
}
37-
core = &SharedCore{plugin: p}
37+
core = &ExtismCore{plugin: p}
3838
}
3939

4040
return core, nil
@@ -44,16 +44,16 @@ func ReleaseCore() {
4444
core = nil
4545
}
4646

47-
// SharedCore implements Core in such a way that all created client instances share the same core resources.
48-
type SharedCore struct {
47+
// ExtismCore implements Core in such a way that all created client instances share the same core resources.
48+
type ExtismCore struct {
4949
// lock is used to synchronize access to the shared WASM core which is single threaded
5050
lock sync.Mutex
5151
// plugin is the Extism plugin which represents the WASM core loaded into memory
5252
plugin *extism.Plugin
5353
}
5454

5555
// InitClient creates a client instance in the current core module and returns its unique ID.
56-
func (c *SharedCore) InitClient(ctx context.Context, config ClientConfig) (*uint64, error) {
56+
func (c *ExtismCore) InitClient(ctx context.Context, config ClientConfig) (*uint64, error) {
5757
marshaledConfig, err := json.Marshal(config)
5858
if err != nil {
5959
return nil, err
@@ -73,7 +73,7 @@ func (c *SharedCore) InitClient(ctx context.Context, config ClientConfig) (*uint
7373
}
7474

7575
// Invoke calls specified business logic from core
76-
func (c *SharedCore) Invoke(ctx context.Context, invokeConfig InvokeConfig) (*string, error) {
76+
func (c *ExtismCore) Invoke(ctx context.Context, invokeConfig InvokeConfig) (*string, error) {
7777
input, err := json.Marshal(invokeConfig)
7878
if err != nil {
7979
return nil, err
@@ -92,7 +92,7 @@ func (c *SharedCore) Invoke(ctx context.Context, invokeConfig InvokeConfig) (*st
9292
}
9393

9494
// ReleaseClient releases memory in the core associated with the given client ID.
95-
func (c *SharedCore) ReleaseClient(clientID uint64) {
95+
func (c *ExtismCore) ReleaseClient(clientID uint64) {
9696
marshaledClientID, err := json.Marshal(clientID)
9797
if err != nil {
9898
c.plugin.Log(extism.LogLevelWarn, fmt.Sprintf("memory couldn't be released: %s", err.Error()))
@@ -103,7 +103,7 @@ func (c *SharedCore) ReleaseClient(clientID uint64) {
103103
}
104104
}
105105

106-
func (c *SharedCore) callWithCtx(ctx context.Context, functionName string, serializedParameters []byte) ([]byte, error) {
106+
func (c *ExtismCore) callWithCtx(ctx context.Context, functionName string, serializedParameters []byte) ([]byte, error) {
107107
c.lock.Lock()
108108
defer c.lock.Unlock()
109109

@@ -114,7 +114,7 @@ func (c *SharedCore) callWithCtx(ctx context.Context, functionName string, seria
114114
return response, nil
115115
}
116116

117-
func (c *SharedCore) call(functionName string, serializedParameters []byte) ([]byte, error) {
117+
func (c *ExtismCore) call(functionName string, serializedParameters []byte) ([]byte, error) {
118118
c.lock.Lock()
119119
defer c.lock.Unlock()
120120

internal/shared_so_core.go renamed to internal/shared_lib_core.go

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log"
99
"os"
10+
"path/filepath"
1011
"runtime"
1112
"unsafe"
1213
)
@@ -18,14 +19,42 @@ import (
1819
#include <stdlib.h>
1920
#include <stdint.h>
2021
21-
// Define a function pointer type matching Rust's send_message
22+
// Function pointer types matching Rust exports
2223
typedef int32_t (*send_message_t)(
2324
const uint8_t* msg_ptr,
2425
size_t msg_len,
2526
uint8_t** out_buf,
26-
size_t* out_len
27+
size_t* out_len,
28+
size_t* out_cap
2729
);
2830
31+
typedef void (*free_message_t)(
32+
uint8_t* buf,
33+
size_t len,
34+
size_t cap
35+
);
36+
37+
// Trampoline wrappers so Go can call the function pointers
38+
static inline int32_t call_send_message(
39+
send_message_t fn,
40+
const uint8_t* msg_ptr,
41+
size_t msg_len,
42+
uint8_t** out_buf,
43+
size_t* out_len,
44+
size_t* out_cap
45+
) {
46+
return fn(msg_ptr, msg_len, out_buf, out_len, out_cap);
47+
}
48+
49+
static inline void call_free_message(
50+
free_message_t fn,
51+
uint8_t* buf,
52+
size_t len,
53+
size_t cap
54+
) {
55+
fn(buf, len, cap);
56+
}
57+
2958
// dlopen wrapper
3059
static void* open_library(const char* path) {
3160
return dlopen(path, RTLD_NOW);
@@ -46,63 +75,77 @@ import "C"
4675
type SharedLibCore struct {
4776
handle unsafe.Pointer
4877
sendMessage C.send_message_t
78+
freeMessage C.free_message_t
4979
}
5080

51-
// Find1PasswordPath tries to return the default installation path of 1Password
52-
func find1PasswordPath() (string, error) {
81+
var coreLib *SharedLibCore
82+
83+
// find1PasswordLibPath returns the path to the 1Password shared library
84+
// (libop_sdk_ipc_client.dylib/.so/.dll) depending on OS.
85+
func find1PasswordLibPath() (string, error) {
5386
switch runtime.GOOS {
5487
case "darwin": // macOS
55-
// 1Password is typically installed as an app bundle
56-
paths := []string{
57-
"/Applications/1Password.app",
88+
// Typical locations for the 1Password bundle
89+
executables := []string{
90+
"/Applications/1Password/Contents/MacOS/1Password",
5891
}
59-
for _, p := range paths {
60-
if _, err := os.Stat(p); err == nil {
61-
return p, nil
92+
93+
for _, exe := range executables {
94+
if _, err := os.Stat(exe); err == nil {
95+
// Replace the executable name with the dylib name
96+
libPath := filepath.Join(filepath.Dir(exe), "libop_sdk_ipc_client.dylib")
97+
if _, err := os.Stat(libPath); err == nil {
98+
return libPath, nil
99+
}
62100
}
63101
}
64102

65103
case "linux":
66-
// Linux installations vary
67-
paths := []string{
68-
"/var/lib/1password",
69-
"/opt/1Password",
104+
// On Linux, it might live under /opt/1Password or similar
105+
candidates := []string{
106+
"/opt/1Password/libop_sdk_ipc_client.so",
107+
"/usr/lib/1password/libop_sdk_ipc_client.so",
70108
}
71-
for _, p := range paths {
72-
if _, err := os.Stat(p); err == nil {
73-
return p, nil
109+
for _, lib := range candidates {
110+
if _, err := os.Stat(lib); err == nil {
111+
return lib, nil
74112
}
75113
}
76114

77115
case "windows":
78-
// On Windows, 1Password installs under Program Files
79-
paths := []string{
116+
// On Windows, shared libs are DLLs in the install directory
117+
executables := []string{
80118
`C:\Program Files\1Password\1Password.exe`,
81119
}
82-
for _, p := range paths {
83-
if _, err := os.Stat(p); err == nil {
84-
return p, nil
120+
for _, exe := range executables {
121+
if _, err := os.Stat(exe); err == nil {
122+
libPath := filepath.Join(filepath.Dir(exe), "op_sdk_ipc_client.dll")
123+
if _, err := os.Stat(libPath); err == nil {
124+
return libPath, nil
125+
}
85126
}
86127
}
87128
}
88129

89-
return "", fmt.Errorf("1Password not found")
130+
return "", fmt.Errorf("1Password desktop application not found")
90131
}
91132

92-
func NewSharedLibCore() (*SharedLibCore, error) {
93-
path, err := find1PasswordPath()
94-
if err != nil {
95-
return nil, err
96-
}
97-
coreLib, err := LoadCore(path)
98-
if err != nil {
99-
return nil, err
133+
func GetSharedLibCore() (*SharedLibCore, error) {
134+
if coreLib == nil {
135+
path, err := find1PasswordLibPath()
136+
if err != nil {
137+
return nil, err
138+
}
139+
coreLib, err = loadCore(path)
140+
if err != nil {
141+
return nil, err
142+
}
100143
}
101144

102145
return coreLib, nil
103146
}
104147

105-
func LoadCore(path string) (*SharedLibCore, error) {
148+
func loadCore(path string) (*SharedLibCore, error) {
106149
cPath := C.CString(path)
107150
defer C.free(unsafe.Pointer(cPath))
108151

@@ -111,18 +154,28 @@ func LoadCore(path string) (*SharedLibCore, error) {
111154
return nil, errors.New("failed to open library")
112155
}
113156

114-
symbol := C.CString("send_message")
157+
symbol := C.CString("op_sdk_ipc_send_message")
115158
defer C.free(unsafe.Pointer(symbol))
116159

117-
fn := C.load_symbol(handle, symbol)
118-
if fn == nil {
160+
fnSend := C.load_symbol(handle, symbol)
161+
if fnSend == nil {
119162
C.close_library(handle)
120163
return nil, errors.New("failed to load send_message")
121164
}
122165

166+
symbolFree := C.CString("op_sdk_ipc_free_response")
167+
defer C.free(unsafe.Pointer(symbolFree))
168+
169+
fnFree := C.load_symbol(handle, symbolFree)
170+
if fnFree == nil {
171+
C.close_library(handle)
172+
return nil, errors.New("failed to load free_message")
173+
}
174+
123175
return &SharedLibCore{
124176
handle: handle,
125-
sendMessage: (C.send_message_t)(fn),
177+
sendMessage: (C.send_message_t)(fnSend),
178+
freeMessage: (C.free_message_t)(fnFree),
126179
}, nil
127180
}
128181

@@ -182,20 +235,24 @@ func (c *SharedLibCore) ReleaseClient(clientID uint64) {
182235
func (c *SharedLibCore) callSharedLibrary(input []byte) ([]byte, error) {
183236
var outBuf *C.uint8_t
184237
var outLen C.size_t
238+
var outCap C.size_t
185239

186-
retCode := c.sendMessage(
240+
retCode := C.call_send_message(
241+
c.sendMessage,
187242
(*C.uint8_t)(unsafe.Pointer(&input[0])),
188243
C.size_t(len(input)),
189244
&outBuf,
190245
&outLen,
246+
&outCap,
191247
)
192248

193249
if retCode != 0 {
194250
return nil, fmt.Errorf("send_message failed: %d", int(retCode))
195251
}
196252

197253
resp := C.GoBytes(unsafe.Pointer(outBuf), C.int(outLen))
198-
C.free(unsafe.Pointer(outBuf))
254+
// Call trampoline with the function pointer
255+
C.call_free_message(c.freeMessage, outBuf, outLen, outCap)
199256

200257
return resp, nil
201258
}

internal/wasm/core.wasm

8.71 MB
Binary file not shown.

secrets.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)