Skip to content

Commit 5b7382e

Browse files
authored
Add use_proxy argument (#255)
* Add use_proxy argument * Revert "Add use_proxy argument" This reverts commit 6218c13. * feat: add no_proxy parameter support with curl-compatible syntax
1 parent 8a35e46 commit 5b7382e

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

mysql/provider.go

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ func Provider() *schema.Provider {
126126
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(socks5h?|http|https)://.*:\d+$`), "The proxy URL is not a valid proxy url. Must be in format: socks5://host:port, http://host:port, or https://host:port"),
127127
},
128128

129+
"no_proxy": {
130+
Type: schema.TypeString,
131+
Optional: true,
132+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
133+
"NO_PROXY",
134+
"no_proxy",
135+
}, nil),
136+
},
137+
129138
"tls": {
130139
Type: schema.TypeString,
131140
Optional: true,
@@ -754,11 +763,95 @@ func (d *httpProxyDialer) Dial(network, addr string) (net.Conn, error) {
754763
return conn, nil
755764
}
756765

766+
func shouldUseProxy(endpoint, noProxy string) bool {
767+
if noProxy == "" {
768+
return true
769+
}
770+
771+
host := endpoint
772+
if strings.Contains(endpoint, ":") {
773+
parts := strings.Split(endpoint, ":")
774+
host = parts[0]
775+
}
776+
777+
for _, pattern := range strings.Split(noProxy, ",") {
778+
pattern = strings.TrimSpace(pattern)
779+
if pattern == "" {
780+
continue
781+
}
782+
783+
// Handle "*" - matches everything
784+
if pattern == "*" {
785+
return false
786+
}
787+
788+
// Handle CIDR notation (e.g., 192.168.0.0/16)
789+
if strings.Contains(pattern, "/") {
790+
_, cidr, err := net.ParseCIDR(pattern)
791+
if err == nil {
792+
ip := net.ParseIP(host)
793+
if ip != nil && cidr.Contains(ip) {
794+
return false
795+
}
796+
}
797+
continue
798+
}
799+
800+
// Handle port-specific patterns like "example.com:8080"
801+
if strings.Contains(pattern, ":") {
802+
if endpoint == pattern {
803+
return false
804+
}
805+
continue
806+
}
807+
808+
// Handle domain patterns
809+
if strings.HasPrefix(pattern, ".") {
810+
// ".example.com" matches "foo.example.com" and "example.com"
811+
domain := pattern[1:]
812+
if host == domain || strings.HasSuffix(host, pattern) {
813+
return false
814+
}
815+
} else if strings.HasPrefix(pattern, "*.") {
816+
// "*.example.com" matches "foo.example.com" but not "example.com"
817+
domain := pattern[2:]
818+
if strings.HasSuffix(host, "."+domain) {
819+
return false
820+
}
821+
} else if strings.Contains(pattern, "*") {
822+
// Simple wildcard matching
823+
matched, _ := regexp.MatchString(strings.ReplaceAll(regexp.QuoteMeta(pattern), `\*`, ".*"), host)
824+
if matched {
825+
return false
826+
}
827+
} else {
828+
// Exact match
829+
if host == pattern {
830+
return false
831+
}
832+
}
833+
}
834+
return true
835+
}
836+
757837
func makeDialer(d *schema.ResourceData) (proxy.Dialer, error) {
758-
proxyFromEnv := proxy.FromEnvironment()
759-
proxyArg := d.Get("proxy").(string)
838+
proxyArg := ""
839+
if v := d.Get("proxy"); v != nil {
840+
proxyArg = v.(string)
841+
}
842+
843+
noProxyArg := ""
844+
if v := d.Get("no_proxy"); v != nil {
845+
noProxyArg = v.(string)
846+
}
847+
848+
endpoint := ""
849+
if v := d.Get("endpoint"); v != nil {
850+
endpoint = v.(string)
851+
}
760852

761-
if len(proxyArg) > 0 {
853+
// Use explicit proxy if configured and not excluded by no_proxy
854+
if len(proxyArg) > 0 && shouldUseProxy(endpoint, noProxyArg) {
762855
proxyURL, err := url.Parse(proxyArg)
763856
if err != nil {
764857
return nil, err
@@ -789,7 +882,8 @@ func makeDialer(d *schema.ResourceData) (proxy.Dialer, error) {
789882
return proxyDialer, nil
790883
}
791884

792-
return proxyFromEnv, nil
885+
// Fall back to environment-based proxy (automatically respects NO_PROXY)
886+
return proxy.FromEnvironment(), nil
793887
}
794888

795889
func quoteIdentifier(in string) string {

website/docs/index.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ The following arguments are supported:
268268
- `username` - Username to use to authenticate with the server, can also be sourced from the `MYSQL_USERNAME` environment variable. This field is optional when `use_rds_data_api` is set to `true` in the `aws_config` block.
269269
- `password` - (Optional) Password for the given user, if that user has a password, can also be sourced from the `MYSQL_PASSWORD` environment variable.
270270
- `proxy` - (Optional) Proxy socks url, can also be sourced from `ALL_PROXY` or `all_proxy` environment variables.
271+
- `no_proxy` - (Optional) Comma-separated list of hosts that should not use the proxy. Supports wildcards (`*.example.com`), domain patterns (`.example.com`), CIDR notation (`192.168.0.0/16`), and exact matches. Can also be sourced from `NO_PROXY` or `no_proxy` environment variables.
271272
- `tls` - (Optional) The TLS configuration. One of `false`, `true`, or `skip-verify`. Defaults to `false`. Can also be sourced from the `MYSQL_TLS_CONFIG` environment variable.
272273
- `custom_tls` - (Optional) Sets custom tls options for the connection. Documentation for encrypted connections can be found [here](https://dev.mysql.com/doc/refman/8.0/en/using-encrypted-connections.html). Consider setting shorter `connect_retry_timeout_sec` for debugging, as the default is 5 minutes .This is a block containing an optional `config_key`, which value is discarded but might be useful when troubleshooting, and the following required arguments:
273274

0 commit comments

Comments
 (0)