Skip to content

Commit ea0fcd5

Browse files
committed
wip
1 parent 036b0ae commit ea0fcd5

3 files changed

Lines changed: 46 additions & 26 deletions

File tree

lib/app/workflow.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ type WorkflowSettings struct {
4848
}
4949

5050
type WorkflowSettingsDomains struct {
51-
Allowed []string `yaml:"allowed"`
51+
Allowed []string `yaml:"allowed"`
52+
Hosts []WorkflowSettingsDomainsHosts `yaml:"hosts"`
53+
}
54+
55+
type WorkflowSettingsDomainsHosts struct {
56+
Hostname string `yaml:"hostname"`
57+
Gateway *string `yaml:"gateway"`
58+
Headers []string `yaml:"headers"`
5259
}
5360

5461
type WorkflowSettingsCache struct {
@@ -180,6 +187,17 @@ func (workflow *StackupWorkflow) configureDefaultSettings() {
180187
workflow.Settings.Domains.Allowed = []string{"raw.githubusercontent.com", "api.github.com"}
181188
}
182189

190+
if len(workflow.Settings.Domains.Hosts) > 0 {
191+
for _, host := range workflow.Settings.Domains.Hosts {
192+
if host.Gateway != nil && *host.Gateway == "allow" {
193+
workflow.Settings.Domains.Allowed = append(workflow.Settings.Domains.Allowed, host.Hostname)
194+
}
195+
if len(host.Headers) > 0 {
196+
App.Gateway.SetDomainHeaders(host.Hostname, host.Headers)
197+
}
198+
}
199+
}
200+
183201
App.Gateway.SetAllowedDomains(workflow.Settings.Domains.Allowed)
184202

185203
if workflow.Settings.Cache.TtlMinutes <= 0 {
@@ -211,13 +229,18 @@ func (workflow *StackupWorkflow) createMissingSettingsSection() {
211229
if workflow.Settings == nil {
212230
verifyChecksums := true
213231
enableStats := false
232+
gatewayAllow := "allowed"
214233
workflow.Settings = &WorkflowSettings{
215234
AnonymousStatistics: &enableStats,
216235
DotEnvFiles: []string{".env"},
217236
Cache: &WorkflowSettingsCache{TtlMinutes: 5},
218237
ChecksumVerification: &verifyChecksums,
219238
Domains: &WorkflowSettingsDomains{
220239
Allowed: []string{"raw.githubusercontent.com", "api.github.com"},
240+
Hosts: []WorkflowSettingsDomainsHosts{
241+
{Hostname: "raw.githubusercontent.com", Gateway: &gatewayAllow, Headers: nil},
242+
{Hostname: "api.github.com", Gateway: &gatewayAllow, Headers: nil},
243+
},
221244
},
222245
Defaults: &WorkflowSettingsDefaults{
223246
Tasks: &WorkflowSettingsDefaultsTasks{
@@ -355,9 +378,9 @@ func (workflow *StackupWorkflow) handleDataNotCached(found bool, data *cache.Cac
355378

356379
if !found || data.IsExpired() {
357380
if include.IsLocalFile() {
358-
include.Contents, err = utils.GetFileContents(include.Filename())
381+
include.Contents, err = App.Gateway.GetUrl(include.Filename())
359382
} else if include.IsRemoteUrl() {
360-
include.Contents, err = utils.GetUrlContentsEx(include.FullUrl(), include.Headers)
383+
include.Contents, err = App.Gateway.GetUrl(include.FullUrl(), include.Headers...)
361384
} else if include.IsS3Url() {
362385
include.AccessKey = os.ExpandEnv(include.AccessKey)
363386
include.SecretKey = os.ExpandEnv(include.SecretKey)

lib/downloader/s3downloader.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,6 @@ func ParseS3Url(urlstr string) (*S3Url, error) {
5757
}
5858

5959
func ReadS3FileContents(s3url string, accessKey string, secretKey string, secure bool) string {
60-
// sess, _ := session.NewSession(&aws.Config{
61-
// Region: aws.String("us-west-2"),
62-
// Credentials: credentials.NewStaticCredentials("AKID", "SECRET_KEY", "TOKEN"),
63-
// })
64-
65-
// writer, err := os.Open(targetFile)
66-
// if err != nil {
67-
// return
68-
// }
69-
// defer writer.Close()
70-
71-
// downloader := s3manager.NewDownloader(sess)
72-
// downloader.Download(writer,
73-
// &s3.GetObjectInput{
74-
// Bucket: &bucket,
75-
// Key: &key,
76-
// })
77-
78-
// fmt.Printf("s3url: %s\n", s3url)
79-
// fmt.Printf("accessKey: %s\n", accessKey)
80-
// fmt.Printf("secretKey: %s\n", secretKey)
81-
8260
data, err := ParseS3Url(s3url)
8361

8462
s3Client, err := minio.New(data.Endpoint, &minio.Options{

lib/gateway/gateway.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/url"
88
"strings"
9+
"sync"
910

1011
glob "github.com/ryanuber/go-glob"
1112
)
@@ -20,6 +21,7 @@ type Gateway struct {
2021
AllowedDomains []string
2122
DeniedDomains []string
2223
Middleware []*GatewayUrlRequestMiddleware
24+
DomainHeaders *sync.Map
2325
}
2426

2527
// New initializes the gateway with deny/allow lists
@@ -29,6 +31,7 @@ func New(deniedDomains, allowedDomains []string) *Gateway {
2931
DeniedDomains: deniedDomains,
3032
AllowedDomains: allowedDomains,
3133
Middleware: []*GatewayUrlRequestMiddleware{},
34+
DomainHeaders: &sync.Map{},
3235
}
3336

3437
result.Initialize()
@@ -56,6 +59,12 @@ func (g *Gateway) SetDeniedDomains(domains []string) {
5659
g.normalizeDataArray(&g.DeniedDomains)
5760
}
5861

62+
func (g *Gateway) SetDomainHeaders(domain string, headers []string) {
63+
for _, header := range headers {
64+
g.DomainHeaders.Store(domain, header)
65+
}
66+
}
67+
5968
func (g *Gateway) AddMiddleware(mw *GatewayUrlRequestMiddleware) {
6069
g.Middleware = append(g.Middleware, mw)
6170
}
@@ -128,11 +137,21 @@ func (g *Gateway) checkArrayForMatch(arr *[]string, s string) bool {
128137
func (g *Gateway) GetUrl(urlStr string, headers ...string) (string, error) {
129138
err := g.runUrlRequestPipeline(urlStr)
130139
if err != nil {
140+
fmt.Printf("error: %v\n", err)
131141
return "", err
132142
}
133143

134144
// remove the header items that are empty strings:
135-
var tempHeaders []string
145+
var tempHeaders []string = []string{"User-Agent: stackup/1.0"}
146+
147+
g.DomainHeaders.Range(func(key, value any) bool {
148+
parsed, _ := url.Parse(urlStr)
149+
if glob.Glob(key.(string), parsed.Hostname()) {
150+
tempHeaders = append(tempHeaders, value.(string))
151+
}
152+
return true
153+
})
154+
136155
for _, header := range headers {
137156
if strings.TrimSpace(header) != "" {
138157
tempHeaders = append(tempHeaders, header)

0 commit comments

Comments
 (0)