-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into cpp_wrapper_for_SDK
- Loading branch information
Showing
16 changed files
with
718 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.