Skip to content

Commit

Permalink
update client
Browse files Browse the repository at this point in the history
  • Loading branch information
lixvxx committed Feb 27, 2023
1 parent 80bea8c commit 3208f48
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 191 deletions.
16 changes: 16 additions & 0 deletions docs/docs/boundary_role.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Table: boundary_role

## Columns

| Column Name | Data Type | Uniq | Nullable | Description |
| ---- | ---- | ---- | ---- | ---- |
| principal_ids | json | X || A list of principal (user or group) IDs to add as principals on the role. |
| scope_id | string | X || The scope ID in which the resource is created. Defaults to the provider's `default_scope` if unset. |
| description | string | X || The role description. |
| grant_scope_id | string | X || |
| grant_strings | json | X || A list of stringified grants for the role. |
| id | string | X || The ID of the role. |
| name | string | X || The role name. Defaults to the resource name. |
| selefra_terraform_original_result | json | X || save terraform original result for compatibility |


1 change: 1 addition & 0 deletions docs/docs/selefra-terraform-provider-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ selefra provider install selefra-terraform-provider-boundary

## Tables

- [boundary_role](boundary_role.md)
- [boundary_auth_method_oidc](boundary_auth_method_oidc.md)
- [boundary_scope](boundary_scope.md)
- [boundary_target](boundary_target.md)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.19

require (
github.com/hashicorp/boundary/api v0.0.34
github.com/joho/godotenv v1.5.1
github.com/selefra/selefra-provider-sdk v0.0.18-0.20230106133742-08876f81b574
github.com/spf13/viper v1.14.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
19 changes: 17 additions & 2 deletions provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ type Config struct {
PassWord string `yaml:"password_auth_method_password" json:"password_auth_method_password" mapstructure:"password_auth_method_password"`
}

func (c *Config) isVaild() bool {
if c.Addr == "" || c.AuthMethodId == "" || c.LoginName == "" || c.PassWord == "" {
return false
}
return true
}

type Client struct {
TerraformBridge *bridge.TerraformBridge
Config
Expand All @@ -39,7 +46,7 @@ func newClient(clientMeta *schema.ClientMeta, config *Config) (*Client, error) {

// Must be four param
// find param in ~/.terraformrc
if config.Addr == "" || config.AuthMethodId == "" || config.LoginName == "" || config.PassWord == "" {
if !config.isVaild() {
homedir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("get param failed: %v", err)
Expand Down Expand Up @@ -67,7 +74,15 @@ func newClient(clientMeta *schema.ClientMeta, config *Config) (*Client, error) {
}
}

if config.Addr == "" || config.AuthMethodId == "" || config.LoginName == "" || config.PassWord == "" {
// Env var
if !config.isVaild() {
config.Addr = os.Getenv("BOUNDARY_ADDR")
config.AuthMethodId = os.Getenv("AUTH_METHOD_ID")
config.LoginName = os.Getenv("PASSWORD_AUTH_METHOD_LOGIN_NAME")
config.PassWord = os.Getenv("PASSWORD_AUTH_METHOD_PASSWORD")
}

if !config.isVaild() {
ErrorF(clientMeta, "Config Error!")
return nil, errors.New("Get Config Error!")
}
Expand Down
100 changes: 91 additions & 9 deletions resources/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,33 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/authmethods"
"github.com/selefra/selefra-provider-sdk/provider/schema"
"github.com/selefra/selefra-provider-sdk/terraform/bridge"
)

type Config struct {
Addr string `yaml:"addr" json:"addr" mapstructure:"addr"`
AuthMethodId string `yaml:"auth_mathod_id" json:"auth_method_id" mapstructure:"auth_method_id"`
LoginName string `yaml:"password_auth_method_login_name" json:"password_auth_method_login_name" mapstructure:"password_auth_method_login_name"`
PassWord string `yaml:"password_auth_method_password" json:"password_auth_method_password" mapstructure:"password_auth_method_password"`
}

func (c *Config) isVaild() bool {
if c.Addr == "" || c.AuthMethodId == "" || c.LoginName == "" || c.PassWord == "" {
return false
}
return true
}

type Client struct {
TerraformBridge *bridge.TerraformBridge
Config

// TODO You can continue to refine your client
ApiClient *api.Client
Expand All @@ -23,9 +41,54 @@ type Client struct {
* New Client, with param: BOUNDARY_ADDR PASSWORD_AUTH_METHOD_PASSWORD PASSWORD_AUTH_METHOD_PASSWORD
* Terrform URL: https://registry.terraform.io/providers/bo
*/
func newClient(clientMeta *schema.ClientMeta) (*Client, error) {
func newClient(clientMeta *schema.ClientMeta, config *Config) (*Client, error) {
fmt.Printf("----------------->\n")

// Must be four param
// find param in ~/.terraformrc
if !config.isVaild() {
homedir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("get param failed: %v", err)
}

rcfContent, err := os.ReadFile(filepath.Join(homedir, ".terraformrc"))
if err != nil {
return nil, fmt.Errorf("get param failed: %v", err)
}
config.Addr, err = getBoundaryParam(rcfContent, "addr")
if err != nil {
return nil, fmt.Errorf("get param addr failed: %v", err)
}
config.AuthMethodId, err = getBoundaryParam(rcfContent, "auth_method_id")
if err != nil {
return nil, fmt.Errorf("get param addr failed: %v", err)
}
config.LoginName, err = getBoundaryParam(rcfContent, "password_auth_method_login_name")
if err != nil {
return nil, fmt.Errorf("get param addr failed: %v", err)
}
config.PassWord, err = getBoundaryParam(rcfContent, "password_auth_method_password")
if err != nil {
return nil, fmt.Errorf("get param addr failed: %v", err)
}
}

// Env var
if !config.isVaild() {
config.Addr = os.Getenv("BOUNDARY_ADDR")
config.AuthMethodId = os.Getenv("AUTH_METHOD_ID")
config.LoginName = os.Getenv("PASSWORD_AUTH_METHOD_LOGIN_NAME")
config.PassWord = os.Getenv("PASSWORD_AUTH_METHOD_PASSWORD")
}

if !config.isVaild() {
ErrorF(clientMeta, "Config Error!")
return nil, errors.New("Get Config Error!")
}

cfg := &api.Config{
Addr: os.Getenv("BOUNDARY_ADDR"),
Addr: config.Addr,
}

// The default address points to the default dev mode address
Expand All @@ -34,27 +97,46 @@ func newClient(clientMeta *schema.ClientMeta) (*Client, error) {
return nil, err
}

token, err := getToken(client)
token, err := getToken(client, config)
if err != nil {
ErrorF(clientMeta, "获取Token信息失败: %s", err.Error())
ErrorF(clientMeta, "Get token error: %s", err.Error())
return nil, err
}

client.SetToken(token)

return &Client{
ApiClient: client,
ApiClient: client,
Config: *config,
}, nil
}

func getToken(client *api.Client) (string, error) {
// input addr = xx12345
// getBoundaryParam(rcfContent, addr) -> return xx12345
func getBoundaryParam(rcfContent []byte, str string) (string, error) {
exp, err := regexp.Compile(fmt.Sprintf(`%s\s?=\s?"?\w+\.\w+.\w+"?`, str))
if err != nil {
return "", fmt.Errorf("get %s failed: %v", str, err)
}

strExp := exp.Find(rcfContent)

rawToken := strings.Split(string(strExp), "=")
if len(rawToken) < 1 {
return "", fmt.Errorf("failed to get boundary str, please set your boundary param correct.")
}
result := strings.TrimSpace(strings.Replace(rawToken[1], "\"", "", -1))
return result, nil
}

func getToken(client *api.Client, config *Config) (string, error) {
credentials := map[string]interface{}{
"login_name": os.Getenv("PASSWORD_AUTH_METHOD_LOGIN_NAME"),
"password": os.Getenv("PASSWORD_AUTH_METHOD_PASSWORD"),
"login_name": config.LoginName,
"password": config.PassWord,
}

amClient := authmethods.NewClient(client)
authenticationResult, err := amClient.Authenticate(context.Background(), os.Getenv("AUTH_METHOD_ID"), "login", credentials)
authenticationResult, err := amClient.Authenticate(context.Background(), config.AuthMethodId, "login", credentials)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 3208f48

Please sign in to comment.