From 066dd681640103197b896f3b38fafa58496aa81c Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 9 Apr 2023 12:54:58 +0200 Subject: [PATCH] create dedicated consul and vault package --- cmd/main.go | 53 ++++++--------------------------------- internal/consul/consul.go | 30 ++++++++++++++++++++++ internal/vault/vault.go | 47 ++++++++++++++++++++++++++++++++++ readme.md | 2 +- 4 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 internal/consul/consul.go create mode 100644 internal/vault/vault.go diff --git a/cmd/main.go b/cmd/main.go index 148c231..cd4c5ed 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,39 +1,25 @@ package main import( - consulapi "github.com/hashicorp/consul/api" "fmt" - "os" - vaultapi "github.com/hashicorp/vault/api" - + "unseal-vault/internal/vault" + "unseal-vault/internal/consul" ) func main() { var key string - consulClient,err := consulapi.NewClient(&consulapi.Config{Address: "consul.service.consul:8500"}) - if err!= nil { - fmt.Println(err.Error()) - os.Exit(1) - } - services:=searchService("vault",consulClient) + consulClient := consul.CreateConsul("consul.service.consul:8500") + services:=consulClient.SearchService("vault") for _, element :=range services { url:=fmt.Sprint("http://",element.ServiceAddress, ":", element.ServicePort) - vaultClient, err:=vaultapi.NewClient(&vaultapi.Config{Address: url}) - if err!= nil { - fmt.Println(err.Error()) - os.Exit(1) - } - if vaultisseal(vaultClient){ + vaultClient:=vault.CreateVault(url) + if vaultClient.Vaultisseal(){ fmt.Println(url + "is sealed") if key == "" { fmt.Print("enter your key: ") fmt.Scan(&key) } - status,err:=vaultunseal(vaultClient,key) - if err != nil { - fmt.Println(err.Error()) - } - if status.Sealed == false { + if vaultClient.Vaultunseal(key){ fmt.Println("unseal success") } }else{ @@ -43,28 +29,3 @@ func main() { } -func searchService(name string,consul *consulapi.Client) []*consulapi.CatalogService{ - catalog:= consul.Catalog() - q := &consulapi.QueryOptions{} - services, _, _ := catalog.Service(name,"",q) - return services -} - -func vaultisseal (vaultClient *vaultapi.Client) (bool){ - sys:=vaultClient.Sys() - health,err:= sys.Health() - if err != nil { - fmt.Println(err.Error()) - return false - } - return health.Sealed - -} - -func vaultunseal (vaultClient *vaultapi.Client,key string) (*vaultapi.SealStatusResponse,error){ - sys:=vaultClient.Sys() - status,err := sys.Unseal(key) - return status,err - - -} diff --git a/internal/consul/consul.go b/internal/consul/consul.go new file mode 100644 index 0000000..7b3ebe1 --- /dev/null +++ b/internal/consul/consul.go @@ -0,0 +1,30 @@ +package consul + +import( + consulapi "github.com/hashicorp/consul/api" + "fmt" + "os" +) + +type Consul struct { + client *consulapi.Client +} + +func CreateConsul (url string) (Consul){ + consul := Consul{} + client,err := consulapi.NewClient(&consulapi.Config{Address:url}) + if err!=nil{ + fmt.Println(err.Error()) + os.Exit(1) + }else{ + consul.client=client + } + return consul +} + +func (c *Consul)SearchService(name string) []*consulapi.CatalogService{ + catalog:= c.client.Catalog() + q := &consulapi.QueryOptions{} + services, _, _ := catalog.Service(name,"",q) + return services +} diff --git a/internal/vault/vault.go b/internal/vault/vault.go new file mode 100644 index 0000000..8315d0d --- /dev/null +++ b/internal/vault/vault.go @@ -0,0 +1,47 @@ +package vault + +import( + vaultapi "github.com/hashicorp/vault/api" + "fmt" + "os" +) + +type Vault struct { + client *vaultapi.Client +} + + +func CreateVault(url string) (Vault) { + vault := Vault{} + client,err := vaultapi.NewClient(&vaultapi.Config{Address:url}) + if err!= nil { + fmt.Println(err.Error()) + os.Exit(1) + }else{ + vault.client=client + } + return vault +} + +func (v *Vault) Vaultisseal () (bool){ + sys:=v.client.Sys() + health,err:= sys.Health() + if err != nil { + fmt.Println(err.Error()) + return false + } + return health.Sealed +} + +func (v *Vault) Vaultunseal (key string) (bool){ + sys:=v.client.Sys() + status,err := sys.Unseal(key) + if err != nil { + fmt.Println(err.Error()) + } + if status.Sealed{ + return false + }else{ + return true + } +} diff --git a/readme.md b/readme.md index a7b3a8d..efe87c4 100644 --- a/readme.md +++ b/readme.md @@ -5,5 +5,5 @@ this program help to unsealed multiple vault instance in one time to do that he ## to do - [ ] manage output in logger -- [ ] create dedicated package for vault and consul function +- [X] create dedicated package for vault and consul function