-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make fileref use root path and stat unit have same logic as embed #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package staticunit | |
|
||
import ( | ||
"errors" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/bmatcuk/doublestar/v4" | ||
"github.com/klothoplatform/klotho/pkg/config" | ||
|
@@ -63,11 +65,16 @@ func (p StaticUnitSplit) Transform(result *core.CompilationResult, deps *core.De | |
matcher := staticAssetPathMatcher{} | ||
matcher.staticFiles, _ = cap.Directives.StringArray("static_files") | ||
matcher.sharedFiles, _ = cap.Directives.StringArray("shared_files") | ||
|
||
err := matcher.ModifyPathsForAnnotatedFile(f.Path()) | ||
if err != nil { | ||
errs.Append(err) | ||
break | ||
} | ||
matchCount := 0 | ||
for _, asset := range input.Files() { | ||
ref := &core.FileRef{ | ||
FPath: asset.Path(), | ||
FPath: asset.Path(), | ||
RootConfigPath: p.Config.Path, | ||
} | ||
static, shared := matcher.Matches(asset.Path()) | ||
if shared { | ||
|
@@ -98,6 +105,47 @@ type staticAssetPathMatcher struct { | |
err error | ||
} | ||
|
||
// ModifyPathsForAnnotatedFile transforms the staticAssetPathMatcher sharedFiles and staticFiles to be absolute paths from the klotho project root, without the prefix '/'. | ||
// This is done to conform to the file path structure of input files. | ||
func (m *staticAssetPathMatcher) ModifyPathsForAnnotatedFile(path string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you doc a bit what this function does? I'm getting lost in the "Abs", "Rel" etc. It likely is similar to how readdir works, but I've forgotten how that works 😄 |
||
newInclude := []string{} | ||
for _, pattern := range m.staticFiles { | ||
absPath, err := filepath.Abs(pattern) | ||
if err != nil { | ||
return err | ||
} | ||
if absPath == pattern { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im not quite understanding why this is a special case. Won't every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so our paths for files are like if they specify an absolute path of The other scenario after that is for relative paths, which wont start with / anyways. The only real reason we check absPath == pattern is just to make sure its a true absolute path. If that doesnt make sense we can rework this or have another discussion, going to push to get the search the deck stuff going for ala |
||
newInclude = append(newInclude, strings.TrimPrefix(pattern, "/")) | ||
continue | ||
} | ||
relPath, err := filepath.Rel(filepath.Dir("."), filepath.Join(filepath.Dir(path), pattern)) | ||
if err != nil { | ||
return err | ||
} | ||
newInclude = append(newInclude, relPath) | ||
} | ||
m.staticFiles = newInclude | ||
|
||
newExclude := []string{} | ||
for _, pattern := range m.sharedFiles { | ||
absPath, err := filepath.Abs(pattern) | ||
if err != nil { | ||
return err | ||
} | ||
if absPath == pattern { | ||
newExclude = append(newExclude, strings.TrimPrefix(pattern, "/")) | ||
continue | ||
} | ||
relPath, err := filepath.Rel(filepath.Dir("."), filepath.Join(filepath.Dir(path), pattern)) | ||
if err != nil { | ||
return err | ||
} | ||
newExclude = append(newExclude, relPath) | ||
} | ||
m.sharedFiles = newExclude | ||
return nil | ||
} | ||
|
||
func (m *staticAssetPathMatcher) Matches(p string) (bool, bool) { | ||
if m.err != nil { | ||
return false, false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing to be mindful of here is that this path is relative to the "root", which is split from the original passed-in path in
splitPathRoot
. This is to prevent plugins from accessing arbitrary files (eg,FPath: "../../../etc/passwd"
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when do we allow access to files outside of the root of the klotho run? Im failing to understand this a little bit so do you have a scenario?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was more a defense against mistakes and third-party plugins (when we get them). Might not be necessary for now, but it does mean this could be incorrect - try doing
klotho ../ --app blah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, let me make a note of that and run through that scenario, if it doesnt work, ill cut a card to fix it