diff --git a/Makefile b/Makefile index 17beaed..aa41e10 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/go.mod b/go.mod index abc8a6c..d6265b2 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index da8d0cd..609a6bf 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main b/main new file mode 100755 index 0000000..f2d8838 Binary files /dev/null and b/main differ diff --git a/pkg/bitwarden/bitwarden.go b/pkg/bitwarden/bitwarden.go new file mode 100644 index 0000000..c4a2a8d --- /dev/null +++ b/pkg/bitwarden/bitwarden.go @@ -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() +} diff --git a/pkg/server/server.go b/pkg/server/server.go index daa82f2..b672d04 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -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 ( @@ -49,7 +52,8 @@ 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")) }) @@ -57,6 +61,7 @@ func (s *Server) Run(_ context.Context) error { s.server = srv if s.Insecure { + slog.Info("starting to listen on http", "addr", s.Addr) return srv.ListenAndServe() }