Skip to content

Commit

Permalink
fix: provider muxing for plugin-framework (#2130)
Browse files Browse the repository at this point in the history
* provider config refactor

* gen docs

* update provider

* update docs

* update provider

* framework initial work

* update rm

* update resource monitor

* update docs

* go fmt

* update provider config

* add to framework repo
  • Loading branch information
sfc-gh-swinkler authored Nov 13, 2023
1 parent 4765410 commit f3c85c0
Show file tree
Hide file tree
Showing 12 changed files with 2,074 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ provider "snowflake" {
- `oauth_endpoint` (String, Sensitive, Deprecated) Required when `oauth_refresh_token` is used. Can also be sourced from `SNOWFLAKE_OAUTH_ENDPOINT` environment variable.
- `oauth_redirect_url` (String, Sensitive, Deprecated) Required when `oauth_refresh_token` is used. Can also be sourced from `SNOWFLAKE_OAUTH_REDIRECT_URL` environment variable.
- `oauth_refresh_token` (String, Sensitive, Deprecated) Token for use with OAuth. Setup and generation of the token is left to other tools. Should be used in conjunction with `oauth_client_id`, `oauth_client_secret`, `oauth_endpoint`, `oauth_redirect_url`. Cannot be used with `browser_auth`, `private_key_path`, `oauth_access_token` or `password`. Can also be sourced from `SNOWFLAKE_OAUTH_REFRESH_TOKEN` environment variable.
- `ocsp_fail_open` (Boolean) True represents OCSP fail open mode. False represents OCSP fail closed mode. Fail open true by default. Can also be sourced from the `SNOWFLAKE_OCSP_FAIL_OPEN` environment variable.
- `okta_url` (String) The URL of the Okta server. e.g. https://example.okta.com. Can also be sourced from the `SNOWFLAKE_OKTA_URL` environment variable.
- `oscp_fail_open` (Boolean) True represents OCSP fail open mode. False represents OCSP fail closed mode. Fail open true by default. Can also be sourced from the `SNOWFLAKE_OCSP_FAIL_OPEN` environment variable.
- `params` (Map of String) Sets other connection (i.e. session) parameters. [Parameters](https://docs.snowflake.com/en/sql-reference/parameters)
- `passcode` (String) Specifies the passcode provided by Duo when using multi-factor authentication (MFA) for login. Can also be sourced from the `SNOWFLAKE_PASSCODE` environment variable.
- `passcode_in_password` (Boolean) False by default. Set to true if the MFA passcode is embedded in the login password. Appends the MFA passcode to the end of the password. Can also be sourced from the `SNOWFLAKE_PASSCODE_IN_PASSWORD` environment variable.
Expand Down
35 changes: 35 additions & 0 deletions framework/planmodifiers/stringplanmodifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package stringplanmodifiers

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)

// useStateForUnknownModifier implements the plan modifier.
type suppressDiffIfModifier struct {
f func(old, new string) bool
}

// Description returns a human-readable description of the plan modifier.
func (m suppressDiffIfModifier) Description(_ context.Context) string {
return "Suppresses diff if values based on function."
}

// MarkdownDescription returns a markdown description of the plan modifier.
func (m suppressDiffIfModifier) MarkdownDescription(_ context.Context) string {
return "Suppresses diff if values based on function."
}

// PlanModifyBool implements the plan modification logic.
func (m suppressDiffIfModifier) PlanModifyString(_ context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
if m.f(req.StateValue.ValueString(), req.PlanValue.ValueString()) {
resp.PlanValue = req.StateValue
}
}

func SuppressDiffIf(f func(old, new string) bool) planmodifier.String {
return suppressDiffIfModifier{
f: f,
}
}
84 changes: 84 additions & 0 deletions framework/provider/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package provider

import (
"os"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/gookit/color"
)

type tfOperation string

const (
CreateOperation tfOperation = "CREATE"
ReadOperation tfOperation = "READ"
UpdateOperation tfOperation = "UPDATE"
DeleteOperation tfOperation = "DELETE"
)

func formatSQLPreview(operation tfOperation, resourceName string, id string, commands []string) string {
var c color.Color
switch operation {
case CreateOperation:
c = color.HiGreen
case ReadOperation:
c = color.HiBlue
case UpdateOperation:
c = color.HiYellow
case DeleteOperation:
c = color.HiRed
}
var sb strings.Builder
sb.WriteString(c.Sprintf("\n[ %s %s %s ]", operation, resourceName, id))
for _, command := range commands {
sb.WriteString(c.Sprintf("\n - %s", command))
}
sb.WriteString("\n")
return sb.String()
}

type sensitiveAttributes struct {
m map[string]bool
}

var (
sa *sensitiveAttributes
lock = sync.Mutex{}
)

func isSensitive(s string) bool {
if sa == nil {
lock.Lock()
defer lock.Unlock()
if sa == nil {
sa = &sensitiveAttributes{
m: make(map[string]bool),
}
dir, err := os.UserHomeDir()
if err != nil {
return false
}
// sensitive path is ~/.snowflake/sensitive.
f := filepath.Join(dir, ".snowflake", "sensitive")
dat, err := os.ReadFile(f)
if err != nil {
return false
}
lines := strings.Split(string(dat), "\n")
r := regexp.MustCompile("(data[.])?snowflake_(.*)[.](.+)[.](.+)")
for _, line := range lines {
strippedLine := strings.TrimSpace(line)
if r.MatchString(strippedLine) {
sa.m[strippedLine] = true
}
}
}
}
if _, ok := sa.m[s]; ok {
return true
}
return false
}
Loading

0 comments on commit f3c85c0

Please sign in to comment.