2019-12-04 17:36:01 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// #include "uplink_definitions.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-17 12:35:17 +00:00
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
2019-12-04 17:36:01 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/macaroon"
|
2019-12-04 17:36:01 +00:00
|
|
|
libuplink "storj.io/storj/lib/uplink"
|
|
|
|
)
|
|
|
|
|
2019-12-10 09:13:25 +00:00
|
|
|
//export new_scope
|
|
|
|
// new_scope creates new Scope
|
|
|
|
func new_scope(satelliteAddress *C.char, apikeyRef C.APIKeyRef, encAccessRef C.EncryptionAccessRef, cerr **C.char) C.ScopeRef {
|
|
|
|
apikey, ok := universe.Get(apikeyRef._handle).(libuplink.APIKey)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid apikey")
|
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
encAccess, ok := universe.Get(encAccessRef._handle).(*libuplink.EncryptionAccess)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid encryption access")
|
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
scope := &libuplink.Scope{
|
|
|
|
SatelliteAddr: C.GoString(satelliteAddress),
|
|
|
|
APIKey: apikey,
|
|
|
|
EncryptionAccess: encAccess,
|
|
|
|
}
|
|
|
|
return C.ScopeRef{_handle: universe.Add(scope)}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export get_scope_satellite_address
|
|
|
|
// get_scope_satellite_address gets Scope satellite address
|
|
|
|
func get_scope_satellite_address(scopeRef C.ScopeRef, cerr **C.char) *C.char {
|
|
|
|
scope, ok := universe.Get(scopeRef._handle).(*libuplink.Scope)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid scope")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(scope.SatelliteAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
//export get_scope_api_key
|
|
|
|
// get_scope_api_key gets Scope APIKey
|
|
|
|
func get_scope_api_key(scopeRef C.ScopeRef, cerr **C.char) C.APIKeyRef {
|
|
|
|
scope, ok := universe.Get(scopeRef._handle).(*libuplink.Scope)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid scope")
|
|
|
|
return C.APIKeyRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.APIKeyRef{_handle: universe.Add(scope.APIKey)}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export get_scope_enc_access
|
|
|
|
// get_scope_enc_access gets Scope encryption access
|
|
|
|
func get_scope_enc_access(scopeRef C.ScopeRef, cerr **C.char) C.EncryptionAccessRef {
|
|
|
|
scope, ok := universe.Get(scopeRef._handle).(*libuplink.Scope)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid scope")
|
|
|
|
return C.EncryptionAccessRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.EncryptionAccessRef{_handle: universe.Add(scope.EncryptionAccess)}
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:36:01 +00:00
|
|
|
//export parse_scope
|
|
|
|
// parse_scope parses an Scope
|
|
|
|
func parse_scope(val *C.char, cerr **C.char) C.ScopeRef {
|
|
|
|
scope, err := libuplink.ParseScope(C.GoString(val))
|
|
|
|
if err != nil {
|
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.ScopeRef{_handle: universe.Add(scope)}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export serialize_scope
|
|
|
|
// serialize_scope serializes the Scope to a string
|
2019-12-10 09:13:25 +00:00
|
|
|
func serialize_scope(scopeRef C.ScopeRef, cerr **C.char) *C.char {
|
|
|
|
scope, ok := universe.Get(scopeRef._handle).(*libuplink.Scope)
|
2019-12-04 17:36:01 +00:00
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid scope")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedScope, err := scope.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(serializedScope)
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:35:17 +00:00
|
|
|
//export restrict_scope
|
|
|
|
// restrict_scope restricts a given scope with the provided caveat and encryption restrictions
|
|
|
|
func restrict_scope(scopeRef C.ScopeRef, caveat C.Caveat, restrictions **C.EncryptionRestriction, restrictionsLen C.size_t, cerr **C.char) C.ScopeRef {
|
|
|
|
scope, ok := universe.Get(scopeRef._handle).(*libuplink.Scope)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid scope")
|
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:36:54 +00:00
|
|
|
irestrictionsLen, ok := safeConvertToInt(restrictionsLen)
|
|
|
|
if !ok || irestrictionsLen < 0 {
|
|
|
|
*cerr = C.CString("invalid restrictionsLen: too large or negative")
|
2019-12-17 12:35:17 +00:00
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
|
|
|
|
|
|
|
caveatGo := macaroon.Caveat{
|
|
|
|
DisallowReads: bool(caveat.disallow_reads),
|
|
|
|
DisallowWrites: bool(caveat.disallow_writes),
|
|
|
|
DisallowLists: bool(caveat.disallow_lists),
|
|
|
|
DisallowDeletes: bool(caveat.disallow_deletes),
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeyRestricted, err := scope.APIKey.Restrict(caveatGo)
|
|
|
|
if err != nil {
|
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
|
|
|
|
2019-12-13 21:36:54 +00:00
|
|
|
restrictionsGo := make([]libuplink.EncryptionRestriction, 0, irestrictionsLen)
|
2019-12-17 12:35:17 +00:00
|
|
|
if restrictions != nil {
|
|
|
|
restrictionsArray := *(*[]C.EncryptionRestriction)(unsafe.Pointer(
|
|
|
|
&reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(restrictions)),
|
2019-12-13 21:36:54 +00:00
|
|
|
Len: irestrictionsLen,
|
|
|
|
Cap: irestrictionsLen,
|
2019-12-17 12:35:17 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
|
|
|
|
for _, restriction := range restrictionsArray {
|
|
|
|
restrictionsGo = append(restrictionsGo, libuplink.EncryptionRestriction{
|
|
|
|
Bucket: C.GoString(restriction.bucket),
|
|
|
|
PathPrefix: C.GoString(restriction.path_prefix),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 16:51:59 +00:00
|
|
|
var encAccessRestricted *libuplink.EncryptionAccess
|
|
|
|
if len(restrictionsGo) > 0 {
|
|
|
|
apiKeyRestricted, encAccessRestricted, err = scope.EncryptionAccess.Restrict(apiKeyRestricted, restrictionsGo...)
|
|
|
|
if err != nil {
|
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
|
|
|
return C.ScopeRef{}
|
|
|
|
}
|
2019-12-17 12:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scopeRestricted := &libuplink.Scope{
|
|
|
|
SatelliteAddr: scope.SatelliteAddr,
|
|
|
|
APIKey: apiKeyRestricted,
|
|
|
|
EncryptionAccess: encAccessRestricted,
|
|
|
|
}
|
|
|
|
return C.ScopeRef{_handle: universe.Add(scopeRestricted)}
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:36:01 +00:00
|
|
|
//export free_scope
|
|
|
|
// free_scope frees an scope
|
2019-12-10 09:13:25 +00:00
|
|
|
func free_scope(scopeRef C.ScopeRef) {
|
|
|
|
universe.Del(scopeRef._handle)
|
2019-12-04 17:36:01 +00:00
|
|
|
}
|