create dedicated consul and vault package

This commit is contained in:
vincent 2023-04-09 12:54:58 +02:00
parent a5341f5df1
commit 066dd68164
4 changed files with 85 additions and 47 deletions

View File

@ -1,39 +1,25 @@
package main package main
import( import(
consulapi "github.com/hashicorp/consul/api"
"fmt" "fmt"
"os" "unseal-vault/internal/vault"
vaultapi "github.com/hashicorp/vault/api" "unseal-vault/internal/consul"
) )
func main() { func main() {
var key string var key string
consulClient,err := consulapi.NewClient(&consulapi.Config{Address: "consul.service.consul:8500"}) consulClient := consul.CreateConsul("consul.service.consul:8500")
if err!= nil { services:=consulClient.SearchService("vault")
fmt.Println(err.Error())
os.Exit(1)
}
services:=searchService("vault",consulClient)
for _, element :=range services { for _, element :=range services {
url:=fmt.Sprint("http://",element.ServiceAddress, ":", element.ServicePort) url:=fmt.Sprint("http://",element.ServiceAddress, ":", element.ServicePort)
vaultClient, err:=vaultapi.NewClient(&vaultapi.Config{Address: url}) vaultClient:=vault.CreateVault(url)
if err!= nil { if vaultClient.Vaultisseal(){
fmt.Println(err.Error())
os.Exit(1)
}
if vaultisseal(vaultClient){
fmt.Println(url + "is sealed") fmt.Println(url + "is sealed")
if key == "" { if key == "" {
fmt.Print("enter your key: ") fmt.Print("enter your key: ")
fmt.Scan(&key) fmt.Scan(&key)
} }
status,err:=vaultunseal(vaultClient,key) if vaultClient.Vaultunseal(key){
if err != nil {
fmt.Println(err.Error())
}
if status.Sealed == false {
fmt.Println("unseal success") fmt.Println("unseal success")
} }
}else{ }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
}

30
internal/consul/consul.go Normal file
View File

@ -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
}

47
internal/vault/vault.go Normal file
View File

@ -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
}
}

View File

@ -5,5 +5,5 @@ this program help to unsealed multiple vault instance in one time to do that he
## to do ## to do
- [ ] manage output in logger - [ ] manage output in logger
- [ ] create dedicated package for vault and consul function - [X] create dedicated package for vault and consul function