Skip to content

Commit

Permalink
working on testing things out and the building process for CGO
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Jun 12, 2024
1 parent d41fbdc commit 8145c72
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 11 deletions.
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,8 @@ GOLANGCI_LINT_VERSION ?= v1.57.2

##@ Build

binaries: ## Builds binaries for all supported platforms, linux, darwin
CGO_ENABLED=0 gox \
-osarch="linux/amd64 linux/arm darwin/amd64" \
-ldflags=${GO_LDFLAGS_STATIC} \
-output="$(BUILDDIR)/{{.OS}}/{{.Arch}}/$(NAME)" \
-tags="netgo" \
./

bootstrap: ## Installs necessary third party components
go get github.com/mitchellh/gox
build: ## Builds binarie
CGO_LDFLAGS="-framework CoreFoundation" CGO_ENABLED=1 go build main.go

##@ Testing

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/external-secrets/bitwarden-sdk-server
go 1.22

require (
github.com/bitwarden/sdk-go v0.1.1
github.com/go-chi/chi/v5 v5.0.12
github.com/gofrs/uuid v4.4.0+incompatible
github.com/spf13/cobra v1.8.0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
github.com/bitwarden/sdk-go v0.1.1 h1:Fn7d0SuThIEwaIecg3SRBM6RUbUyQQ7x7Ex+qrcLbMA=
github.com/bitwarden/sdk-go v0.1.1/go.mod h1:Gp2ADXAL0XQ3GO3zxAv503xSlL6ORPf0VZg2J+yQ6jU=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
Binary file added main
Binary file not shown.
130 changes: 130 additions & 0 deletions pkg/bitwarden/bitwarden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bitwarden

import (
"encoding/json"
"fmt"
"log"
"os"

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

func GetSecret() {
// Configuring the URLS is optional, set them to nil to use the default values
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")

// Configuring the statePath is optional, pass nil
// in AccessTokenLogin() to not use state
statePath := os.Getenv("STATE_PATH")

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

err := bitwardenClient.AccessTokenLogin(accessToken, &statePath)
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)
}

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

// Get secrets with a list of IDs
secretIDs := make([]string, len(secretIdentifiers.Data))
for i, identifier := range secretIdentifiers.Data {
secretIDs[i] = identifier.ID
}

secrets, err := bitwardenClient.Secrets().GetByIDS(secretIDs)
if err != nil {
log.Fatalf("Error getting secrets: %v", err)
}

jsonSecrets, err := json.MarshalIndent(secrets, "", " ")
if err != nil {
log.Fatalf("Error marshalling secrets to JSON: %v", err)
}

fmt.Println(string(jsonSecrets))

defer bitwardenClient.Close()
}
7 changes: 6 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ package server

import (
"context"
"log/slog"
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"

"github.com/external-secrets/bitwarden-sdk-server/pkg/bitwarden"
)

const (
Expand Down Expand Up @@ -49,14 +52,16 @@ func NewServer(cfg Config) *Server {
func (s *Server) Run(_ context.Context) error {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get(api, func(w http.ResponseWriter, r *http.Request) {
r.Get(api+"/get/secret", func(w http.ResponseWriter, r *http.Request) {
bitwarden.GetSecret()
_, _ = w.Write([]byte("welcome"))
})

srv := &http.Server{Addr: s.Addr, Handler: r, ReadTimeout: 5 * time.Second}
s.server = srv

if s.Insecure {
slog.Info("starting to listen on http", "addr", s.Addr)
return srv.ListenAndServe()
}

Expand Down

0 comments on commit 8145c72

Please sign in to comment.