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

Golang SDK #218

Merged
merged 41 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
770f95c
initial
cvele Sep 4, 2023
c058c7f
Merge branch 'master' into golang-sdk
cvele Sep 4, 2023
9c5c1f1
Adding basic readme
cvele Sep 4, 2023
7061221
Renaming go package
cvele Sep 5, 2023
8ba4987
Merge branch 'master' into golang-sdk
cvele Sep 5, 2023
a277dfd
ignoring auto-generated schema
cvele Sep 6, 2023
5260304
Merge branch 'golang-sdk' of github.com:cvele/sdk into golang-sdk
cvele Sep 6, 2023
4742e1c
ignoring auto-generated schema
cvele Sep 6, 2023
c19b5ed
Do not expose Impl to consumers
cvele Oct 2, 2023
64dbda5
Clearly denote file as example
cvele Oct 2, 2023
4f30dac
modify package name from main to sdk
cvele Oct 2, 2023
35f1601
Moving c interface to internal package
cvele Oct 2, 2023
537e10b
Exclude unsafe internal impl from examples
cvele Oct 2, 2023
12f295c
Merge branch 'master' into golang-sdk
cvele Oct 2, 2023
af0b153
lower version requirement
cvele Oct 2, 2023
8c8bc29
WIP - initial github release action
cvele Oct 2, 2023
3da0798
fix spelling
cvele Oct 26, 2023
e7e6a3c
Fix package name in schemas
cvele Oct 26, 2023
93eadd1
Update example, remove hard dependency on schemas
cvele Oct 26, 2023
9b4359a
initialize example main module
cvele Oct 26, 2023
f8342c2
Be friendly to IDE - add .work
cvele Oct 26, 2023
574a832
Unwrap the responses
cvele Oct 26, 2023
18cdf6c
Unwrap the responses
cvele Oct 26, 2023
29a86e6
Unwrap responses and return native error types
cvele Oct 26, 2023
4dc0fd6
Merge branch 'master' into golang-sdk
cvele Oct 26, 2023
2f5bd02
address PR comments
Oct 30, 2023
4e98474
address PR comments
Oct 31, 2023
00ca7a3
address PR comments
Oct 31, 2023
b9060c4
address PR comments
Oct 31, 2023
5ce40b4
remove go.work
Nov 2, 2023
afa2813
address PR comments
Nov 2, 2023
b7ef4b8
remove build.sh and custom lib
Nov 14, 2023
6a04b18
Merge branch 'master' into golang-sdk
bbujak Nov 22, 2023
d48f419
Merge branch 'master' into golang-sdk
bbujak Nov 24, 2023
302729a
Merge branch 'master' into golang-sdk
bbujak Nov 27, 2023
4c9f6db
remove some comments
Nov 27, 2023
3640a1f
Merge branch 'master' into golang-sdk
bbujak Nov 28, 2023
9257c20
run prettier
Nov 28, 2023
21669a4
Merge remote-tracking branch 'origin/golang-sdk' into golang-sdk
Nov 28, 2023
1dc8a3b
Merge branch 'master' into golang-sdk
bbujak Nov 30, 2023
5c14d95
merged main
Nov 30, 2023
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
6 changes: 6 additions & 0 deletions .github/workflows/generate_schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ jobs:
path: ${{ github.workspace }}/support/schemas/*
if-no-files-found: error

- name: Upload Go schemas artifact
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: schemas.go
path: ${{ github.workspace }}/languages/go/schema.go

- name: Upload java schemas artifact
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
Expand Down
73 changes: 73 additions & 0 deletions .github/workflows/golang-release.yml
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Go Release

on:
workflow_dispatch:
inputs:
version_number:
description: "New Version"
required: true

env:
GO111MODULE: on
GO_VERSION: "^1.18"

jobs:
build_rust:
uses: ./.github/workflows/build-rust-cross-platform.yml

generate-schemas:
uses: ./.github/workflows/generate_schemas.yml

build:
name: Build
needs:
- build_rust
- generate-schemas
runs-on: ubuntu-22.04
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Setup Go environment
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: ${{ env.GO_VERSION }}

- name: Cache dependencies
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
mimartin12 marked this conversation as resolved.
Show resolved Hide resolved

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...

release:
name: Release
needs: build
runs-on: ubuntu-22.04
steps:
- name: Checkout Repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Setup Go environment
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: ${{ env.GO_VERSION }}

- name: Set release version
run: echo "VERSION=${{ github.event.inputs.version_number }}" >> $GITHUB_ENV

- name: Install Goreleaser
run: go install github.com/goreleaser/[email protected]

- name: Run Goreleaser
run: goreleaser release --rm-dist --skip-validate
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ env.VERSION }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ crates/bitwarden-napi/src-ts/bitwarden_client/schemas.ts
languages/csharp/Bitwarden.Sdk/schemas.cs
languages/js_webassembly/bitwarden_client/schemas.ts
languages/python/BitwardenClient/schemas.py
languages/go/schema.go
languages/java/src/main/java/com/bitwarden/sdk/schema
113 changes: 113 additions & 0 deletions languages/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Bitwarden SDK in Go

This SDK is designed to interact with Bitwarden services in Go. It includes implementations for
managing projects and secrets, as well as a client interface to facilitate operations like login.

## Prerequisites

- Go installed
- C environment to run CGO

## Installation

Download the SDK files and place them in your Go project directory.
Hinton marked this conversation as resolved.
Show resolved Hide resolved

## Table of Contents

- [Initialization](#initialization)
- [Login](#login)
- [Projects](#projects)
- [Secrets](#secrets)
- [Close Client](#close-client)

---

### Initialization

To initialize the client, you need to import the SDK and create a new `BitwardenClient` instance.

```go
import "github.com/bitwarden/sdk/languages/go"

bitwardenClient, _ := sdk.NewBitwardenClient(&apiURL, &identityURL)
```

---

### Login

To login using an access token:

```go
apiKeyLogin, err := bitwardenClient.AccessTokenLogin(accessToken)
```

---

### Projects

#### Create a Project

```go
project, err := client.Projects.Create("organization_id", "project_name")
```

#### List Projects

```go
projects, err := client.Projects.List("organization_id")
```

#### Update a Project

```go
project, err := client.Projects.Update("project_id", "organization_id", "new_project_name")
```

#### Delete Projects

```go
project, err := client.Projects.Delete([]string{"project_id_1", "project_id_2"})
```

---

### Secrets

#### Create a Secret

```go
secret, err := client.Secrets.Create("key", "value", "note", "organization_id", []string{"project_id"})
```

#### List Secrets

```go
secrets, err := client.Secrets.List("organization_id")
```

#### Update a Secret

```go
secret, err := client.Secrets.Update("secret_id", "new_key", "new_value", "new_note", "organization_id", []string{"project_id"})
```

#### Delete Secrets

```go
secret, err := client.Secrets.Delete([]string{"secret_id_1", "secret_id_2"})
```

---

### Close Client

To free up resources:

```go
defer bitwardenClient.Close()
```

---

For more detailed information, refer to the code comments and method signatures.
63 changes: 63 additions & 0 deletions languages/go/bitwarden_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package sdk

import (
"encoding/json"

"github.com/bitwarden/sdk/languages/go/internal/cinterface"
)

type BitwardenClient struct {
client cinterface.ClientPointer
lib cinterface.BitwardenLibrary
commandRunner CommandRunnerInterface
Projects ProjectsInterface
Secrets SecretsInterface
}

func NewBitwardenClient(apiURL *string, identityURL *string) (*BitwardenClient, error) {
deviceType := DeviceType("SDK")
userAgent := "Bitwarden GOLANG-SDK"
clientSettings := ClientSettings{
APIURL: apiURL,
IdentityURL: identityURL,
UserAgent: &userAgent,
DeviceType: &deviceType,
}

settingsJSON, err := json.Marshal(clientSettings)
if err != nil {
return nil, err
}

lib := cinterface.NewBitwardenLibrary()
client, err := lib.Init(string(settingsJSON))
if err != nil {
return nil, err
}
runner := NewCommandRunner(client, lib)

return &BitwardenClient{
lib: lib,
client: client,
commandRunner: runner,
Projects: NewProjects(runner),
Secrets: NewSecrets(runner),
}, nil
}

func (c *BitwardenClient) AccessTokenLogin(accessToken string) error {
req := AccessTokenLoginRequest{AccessToken: accessToken}
command := Command{AccessTokenLogin: &req}

responseStr, err := c.commandRunner.RunCommand(command)
if err != nil {
return err
}

var response APIKeyLoginResponse
return checkSuccessAndError(responseStr, &response)
}

func (c *BitwardenClient) Close() {
c.lib.FreeMem(c.client)
}
37 changes: 37 additions & 0 deletions languages/go/command_runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sdk

import (
"encoding/json"

"github.com/bitwarden/sdk/languages/go/internal/cinterface"
)

type CommandRunnerInterface interface {
RunCommand(command Command) (string, error)
}

type CommandRunner struct {
client cinterface.ClientPointer
lib cinterface.BitwardenLibrary
}

func NewCommandRunner(client cinterface.ClientPointer, lib cinterface.BitwardenLibrary) *CommandRunner {
return &CommandRunner{
client: client,
lib: lib,
}
}

func (c *CommandRunner) RunCommand(command Command) (string, error) {
commandJSON, err := json.Marshal(command)
if err != nil {
return "", err
}

responseStr, err := c.lib.RunCommand(string(commandJSON), c.client)
if err != nil {
return "", err
}

return responseStr, nil
}
86 changes: 86 additions & 0 deletions languages/go/example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"os"

sdk "github.com/bitwarden/sdk/languages/go"
"github.com/gofrs/uuid"
)

func main() {
apiURL := os.Getenv("API_URL")
identityURL := os.Getenv("IDENTITY_URL")

bitwardenClient, _ := sdk.NewBitwardenClient(&apiURL, &identityURL)

accessToken := os.Getenv("ACCESS_TOKEN")
organizationIDStr := os.Getenv("ORGANIZATION_ID")
projectName := os.Getenv("PROJECT_NAME")

if projectName == "" {
projectName = "NewTestProject" // default value
}

err := bitwardenClient.AccessTokenLogin(accessToken)
if err != nil {
panic(err)
}

organizationID, err := uuid.FromString(organizationIDStr)
if err != nil {
panic(err)
}

project, err := bitwardenClient.Projects.Create(organizationID.String(), projectName)
if err != nil {
panic(err)
}
fmt.Println(project)
projectID := project.ID
fmt.Println(projectID)

if _, err = bitwardenClient.Projects.List(organizationID.String()); err != nil {
panic(err)
}

if _, err = bitwardenClient.Projects.Get(projectID); err != nil {
panic(err)
}

if _, err = bitwardenClient.Projects.Update(projectID, organizationID.String(), projectName+"2"); err != nil {
panic(err)
}

key := "key"
value := "value"
note := "note"

secret, err := bitwardenClient.Secrets.Create(key, value, note, organizationID.String(), []string{projectID})
if err != nil {
panic(err)
}
secretID := secret.ID

if _, err = bitwardenClient.Secrets.List(organizationID.String()); err != nil {
panic(err)
}

if _, err = bitwardenClient.Secrets.Get(secretID); err != nil {
panic(err)
}

if _, err = bitwardenClient.Secrets.Update(secretID, key, value, note, organizationID.String(), []string{projectID}); err != nil {
panic(err)
}

if _, err = bitwardenClient.Secrets.Delete([]string{secretID}); err != nil {
panic(err)
}

if _, err = bitwardenClient.Projects.Delete([]string{projectID}); err != nil {
panic(err)
}

defer bitwardenClient.Close()
}
Loading
Loading