Skip to content

Commit

Permalink
Merge branch 'master' into cpp_wrapper_for_SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia authored Nov 30, 2023
2 parents dd62664 + 9f77596 commit ba2e2e4
Show file tree
Hide file tree
Showing 16 changed files with 718 additions and 0 deletions.
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
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-
- 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 @@ -51,4 +51,5 @@ languages/csharp/Bitwarden.Sdk/schemas.cs
languages/js_webassembly/bitwarden_client/schemas.ts
languages/python/BitwardenClient/schemas.py
languages/cpp/include/schemas.hpp
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.

## 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

0 comments on commit ba2e2e4

Please sign in to comment.