create dedicated consul and vault package
This commit is contained in:
parent
a5341f5df1
commit
066dd68164
53
cmd/main.go
53
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
|
||||
|
||||
|
||||
}
|
||||
|
30
internal/consul/consul.go
Normal file
30
internal/consul/consul.go
Normal 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
47
internal/vault/vault.go
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user