Skip to content

Commit

Permalink
Add HTTP basic auth environment variables (#231)
Browse files Browse the repository at this point in the history
* add basic auth env var #226
* update README for #226

Signed-off-by: Denis Vaumoron <[email protected]>
  • Loading branch information
dvaumoron authored Aug 18, 2024
1 parent 8b668e2 commit 79f6ef3
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 141 deletions.
80 changes: 77 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,24 @@ See [advanced remote configuration](#advanced-remote-configuration) for more det
</details>


<details><summary><b>TOFUENV_REMOTE_PASSWORD</b></summary><br>

String (Default: "")

Could be used with TOFUENV_REMOTE_USER to specify HTTP basic auth when same credential are used with TOFUENV_REMOTE and TOFUENV_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>TOFUENV_REMOTE_USER</b></summary><br>

String (Default: "")

Could be used with TOFUENV_REMOTE_PASSWORD to specify HTTP basic auth when same credential are used with TOFUENV_REMOTE and TOFUENV_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>TOFUENV_ROOT</b></summary><br>

Same as TENV_ROOT (compatibility with [tofuenv](https://github.com/tofuutils/tofuenv)).
Expand All @@ -817,10 +835,10 @@ Same as TENV_ROOT (compatibility with [tofuenv](https://github.com/tofuutils/tof

<details><summary><b>TOFUENV_URL_TEMPLATE</b></summary><br>

String (Default: "https://github.com/opentofu/opentofu/releases/download/v{{ .Version }}/{{ .Artifact }}")


String (Default: `https://github.com/opentofu/opentofu/releases/download/v{{ .Version }}/{{ .Artifact }}`)

Used when TOFUENV_INSTALL_MODE is "mirror" (see [TofuDL mirror specification](https://github.com/opentofu/tofudl/blob/mirror-spec/MIRROR-SPECIFICATION.md)).

</details>


Expand Down Expand Up @@ -959,6 +977,24 @@ See [advanced remote configuration](#advanced-remote-configuration) for more det
</details>


<details><summary><b>TFENV_REMOTE_PASSWORD</b></summary><br>

String (Default: "")

Could be used with TFENV_REMOTE_USER to specify HTTP basic auth when same credential are used with TFENV_REMOTE and TFENV_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>TFENV_REMOTE_USER</b></summary><br>

String (Default: "")

Could be used with TFENV_REMOTE_PASSWORD to specify HTTP basic auth when same credential are used with TFENV_REMOTE and TFENV_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>TFENV_ROOT</b></summary><br>

Same as TENV_ROOT (compatibility with [tfenv](https://github.com/tfutils/tfenv)).
Expand Down Expand Up @@ -1065,6 +1101,25 @@ See [advanced remote configuration](#advanced-remote-configuration) for more det

</details>


<details><summary><b>TG_REMOTE_PASSWORD</b></summary><br>

String (Default: "")

Could be used with TG_REMOTE_USER to specify HTTP basic auth when same credential are used with TG_REMOTE and TG_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>TG_REMOTE_USER</b></summary><br>

String (Default: "")

Could be used with TG_REMOTE_PASSWORD to specify HTTP basic auth when same credential are used with TG_REMOTE and TG_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>TG_DEFAULT_CONSTRAINT</b></summary><br>

String (Default: "")
Expand Down Expand Up @@ -1158,6 +1213,25 @@ See [advanced remote configuration](#advanced-remote-configuration) for more det

</details>


<details><summary><b>ATMOS_REMOTE_PASSWORD</b></summary><br>

String (Default: "")

Could be used with ATMOS_REMOTE_USER to specify HTTP basic auth when same credential are used with ATMOS_REMOTE and ATMOS_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>ATMOS_REMOTE_USER</b></summary><br>

String (Default: "")

Could be used with ATMOS_REMOTE_PASSWORD to specify HTTP basic auth when same credential are used with ATMOS_REMOTE and ATMOS_LIST_URL (instead of `https://user:[email protected]` URL format).

</details>


<details><summary><b>ATMOS_DEFAULT_CONSTRAINT</b></summary><br>

String (Default: "")
Expand Down
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ const (
listURLEnvName = "LIST_URL"
logEnvName = "LOG"
quietEnvName = "QUIET"
remotePassEnvName = "REMOTE_PASSWORD"
remoteURLEnvName = "REMOTE"
remoteUserEnvName = "REMOTE_USER"
rootPathEnvName = "ROOT"
tokenEnvName = "GITHUB_TOKEN" //nolint
version = "VERSION"
Expand All @@ -58,7 +60,9 @@ const (
atmosInstallModeEnvName = atmosPrefix + installModeEnvName
atmosListModeEnvName = atmosPrefix + listModeEnvName
atmosListURLEnvName = atmosPrefix + listURLEnvName
AtmosRemotePassEnvName = atmosPrefix + remotePassEnvName
AtmosRemoteURLEnvName = atmosPrefix + remoteURLEnvName
AtmosRemoteUserEnvName = atmosPrefix + remoteUserEnvName
AtmosVersionEnvName = atmosPrefix + version

tenvPrefix = "TENV_"
Expand All @@ -82,7 +86,9 @@ const (
tfInstallModeEnvName = tfenvPrefix + installModeEnvName
tfListModeEnvName = tfenvPrefix + listModeEnvName
tfListURLEnvName = tfenvPrefix + listURLEnvName
TfRemotePassEnvName = tfenvPrefix + remotePassEnvName
TfRemoteURLEnvName = tfenvPrefix + remoteURLEnvName
TfRemoteUserEnvName = tfenvPrefix + remoteUserEnvName
tfRootPathEnvName = tfenvPrefix + rootPathEnvName
TfVersionEnvName = tfenvTerraformPrefix + version

Expand All @@ -92,7 +98,9 @@ const (
tgInstallModeEnvName = tgPrefix + installModeEnvName
tgListModeEnvName = tgPrefix + listModeEnvName
tgListURLEnvName = tgPrefix + listURLEnvName
TgRemotePassEnvName = tgPrefix + remotePassEnvName
TgRemoteURLEnvName = tgPrefix + remoteURLEnvName
TgRemoteUserEnvName = tgPrefix + remoteUserEnvName
TgVersionEnvName = tgPrefix + version

tofuenvPrefix = "TOFUENV_"
Expand All @@ -106,7 +114,9 @@ const (
tofuListModeEnvName = tofuenvPrefix + listModeEnvName
tofuListURLEnvName = tofuenvPrefix + listURLEnvName
tofuOpenTofuPGPKeyEnvName = tofuenvPrefix + "OPENTOFU_PGP_KEY"
TofuRemotePassEnvName = tofuenvPrefix + remotePassEnvName
TofuRemoteURLEnvName = tofuenvPrefix + remoteURLEnvName
TofuRemoteUserEnvName = tofuenvPrefix + remoteUserEnvName
tofuRootPathEnvName = tofuenvPrefix + rootPathEnvName
tofuTokenEnvName = tofuenvPrefix + tokenEnvName
TofuURLTemplateEnvName = tofuenvPrefix + "URL_TEMPLATE"
Expand Down
11 changes: 11 additions & 0 deletions config/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"errors"
"os"
"strings"

"github.com/tofuutils/tenv/v3/pkg/download"
)

const (
Expand Down Expand Up @@ -135,3 +137,12 @@ func MapGetDefault(m map[string]string, key string, defaultValue string) string

return defaultValue
}

func GetBasicAuthOption(userEnvName string, passEnvName string) []download.RequestOption {
username, password := os.Getenv(userEnvName), os.Getenv(passEnvName)
if username == "" || password == "" {
return nil
}

return []download.RequestOption{download.WithBasicAuth(username, password)}
}
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ require (
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.24.0 // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -158,8 +158,8 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -173,14 +173,14 @@ golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
38 changes: 36 additions & 2 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
package download

import (
"encoding/json"
"io"
"net/http"
"net/url"
)

type RequestOption = func(*http.Request) *http.Request

func ApplyUrlTranformer(urlTransformer func(string) (string, error), baseURLs ...string) ([]string, error) {
transformedURLs := make([]string, 0, len(baseURLs))
for _, baseURL := range baseURLs {
Expand All @@ -38,10 +41,19 @@ func ApplyUrlTranformer(urlTransformer func(string) (string, error), baseURLs ..
return transformedURLs, nil
}

func Bytes(url string, display func(string)) ([]byte, error) {
func Bytes(url string, display func(string), requestOptions ...RequestOption) ([]byte, error) {
display("Downloading " + url)

response, err := http.Get(url) //nolint
request, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
return nil, err
}

for _, option := range requestOptions {
request = option(request)
}

response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
Expand All @@ -50,6 +62,20 @@ func Bytes(url string, display func(string)) ([]byte, error) {
return io.ReadAll(response.Body)
}

func JSON(url string, display func(string), requestOptions ...RequestOption) (any, error) {
data, err := Bytes(url, display, requestOptions...)
if err != nil {
return nil, err
}

var value any
err = json.Unmarshal(data, &value)

return value, err
}

func NoDisplay(string) {}

func UrlTranformer(rewriteRule []string) func(string) (string, error) {
if len(rewriteRule) < 2 {
return noTransform
Expand All @@ -71,6 +97,14 @@ func UrlTranformer(rewriteRule []string) func(string) (string, error) {
}
}

func WithBasicAuth(username string, password string) RequestOption {
return func(r *http.Request) *http.Request {
r.SetBasicAuth(username, password)

return r
}
}

func noTransform(value string) (string, error) {
return value, nil
}
Loading

0 comments on commit 79f6ef3

Please sign in to comment.