Skip to content

Commit

Permalink
[SM-1214] Add secrets syncing support to Go (#742)
Browse files Browse the repository at this point in the history
## Type of change

<!-- (mark with an `X`) -->

```
- [ ] Bug fix
- [X] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective

<!--Describe what the purpose of this PR is. For example: what bug
you're fixing or what new feature you're adding-->
Expose secret syncing to the Go language wrapper.

## Code changes

<!--Explain the changes you've made to each file or major component.
This should help the reviewer understand your changes-->
<!--Also refer to any related changes or PRs in other repositories-->

- **crates/bitwarden-json/src/client.rs:** 
**crates/bitwarden-json/src/command.rs:** 
**crates/sdk-schemas/src/main.rs:** 
Expose secrets sync in schemas


- **languages/go/README.md:**
**languages/go/secrets.go:**
Add secrets sync in Go language wrapper.

## Before you submit

- Please add **unit tests** where it makes sense to do so
  • Loading branch information
Thomas-Avery authored May 16, 2024
1 parent 295d3c5 commit be84bca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/bitwarden-json/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Client {
SecretsCommand::List(req) => client.secrets().list(&req).await.into_string(),
SecretsCommand::Update(req) => client.secrets().update(&req).await.into_string(),
SecretsCommand::Delete(req) => client.secrets().delete(req).await.into_string(),
SecretsCommand::Sync(req) => client.secrets().sync(&req).await.into_string(),
},

#[cfg(feature = "secrets")]
Expand Down
12 changes: 11 additions & 1 deletion crates/bitwarden-json/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitwarden::{
},
secrets::{
SecretCreateRequest, SecretGetRequest, SecretIdentifiersRequest, SecretPutRequest,
SecretsDeleteRequest, SecretsGetRequest,
SecretsDeleteRequest, SecretsGetRequest, SecretsSyncRequest,
},
},
};
Expand Down Expand Up @@ -123,6 +123,16 @@ pub enum SecretsCommand {
///
/// Returns: [SecretsDeleteResponse](bitwarden::secrets_manager::secrets::SecretsDeleteResponse)
Delete(SecretsDeleteRequest),

/// > Requires Authentication
/// > Requires using an Access Token for login
/// Retrieve the secrets accessible by the authenticated machine account
/// Optionally, provide the last synced date to assess whether any changes have occurred
/// If changes are detected, retrieves all the secrets accessible by the authenticated machine
/// account
///
/// Returns: [SecretsSyncResponse](bitwarden::secrets_manager::secrets::SecretsSyncResponse)
Sync(SecretsSyncRequest),
}

#[cfg(feature = "secrets")]
Expand Down
1 change: 1 addition & 0 deletions crates/sdk-schemas/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ struct SchemaTypes {
secret: Response<bitwarden::secrets_manager::secrets::SecretResponse>,
secrets: Response<bitwarden::secrets_manager::secrets::SecretsResponse>,
secrets_delete: Response<bitwarden::secrets_manager::secrets::SecretsDeleteResponse>,
secrets_sync: Response<bitwarden::secrets_manager::secrets::SecretsSyncResponse>,
project: Response<bitwarden::secrets_manager::projects::ProjectResponse>,
projects: Response<bitwarden::secrets_manager::projects::ProjectsResponse>,
projects_delete: Response<bitwarden::secrets_manager::projects::ProjectsDeleteResponse>,
Expand Down
9 changes: 9 additions & 0 deletions languages/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ secret, err := client.Secrets.Update("secret_id", "new_key", "new_value", "new_n
secret, err := client.Secrets.Delete([]string{"secret_id_1", "secret_id_2"})
```

#### Secrets Sync

```go
secretsSync, err := client.Secrets.Sync("organization_id", nil)

lastSyncedDate := time.Now()
secretsSync, err := client.Secrets.Sync("organization_id", lastSyncedDate)
```

---

### Close Client
Expand Down
26 changes: 26 additions & 0 deletions languages/go/secrets.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package sdk

import "time"

type SecretsInterface interface {
Create(key, value, note string, organizationID string, projectIDs []string) (*SecretResponse, error)
List(organizationID string) (*SecretIdentifiersResponse, error)
Get(secretID string) (*SecretResponse, error)
GetByIDS(secretIDs []string) (*SecretsResponse, error)
Update(secretID string, key, value, note string, organizationID string, projectIDs []string) (*SecretResponse, error)
Delete(secretIDs []string) (*SecretsDeleteResponse, error)
Sync(organizationID string, lastSyncedDate *time.Time) (*SecretsSyncResponse, error)
}

type Secrets struct {
Expand Down Expand Up @@ -129,3 +132,26 @@ func (s *Secrets) Delete(ids []string) (*SecretsDeleteResponse, error) {
}
return &response, nil
}

func (s *Secrets) Sync(organizationID string, lastSyncedDate *time.Time) (*SecretsSyncResponse, error) {
var lastSyncedDateString *string
if lastSyncedDate != nil {
tempRfc3339 := lastSyncedDate.UTC().Format(time.RFC3339)
lastSyncedDateString = &tempRfc3339
}

command := Command{
Secrets: &SecretsCommand{
Sync: &SecretsSyncRequest{
OrganizationID: organizationID,
LastSyncedDate: lastSyncedDateString,
},
},
}

var response SecretsSyncResponse
if err := s.executeCommand(command, &response); err != nil {
return nil, err
}
return &response, nil
}

0 comments on commit be84bca

Please sign in to comment.