diff --git a/pkg/provider/download.go b/pkg/provider/download.go index f7add5319..6ace9ea4b 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -73,8 +73,8 @@ func GetBinariesFrom(config *ProviderConfig, binariesDir string) (map[string]str } targetFolder := filepath.Join(binariesDir, strings.ToLower(binaryName)) - binaryPath := getBinaryPath(binary, targetFolder) - if _, err := os.Stat(binaryPath); err != nil { + binaryPath, err := findBinaryPath(binary, targetFolder) + if err != nil { return nil, fmt.Errorf("error trying to find binary %s: %w", binaryName, err) } @@ -95,6 +95,15 @@ func GetBinariesFrom(config *ProviderConfig, binariesDir string) (map[string]str return retBinaries, nil } +func findBinaryPath(binary *ProviderBinary, targetFolder string) (string, error) { + binaryPath := getBinaryPath(binary, targetFolder) + if isUnownedLocalBinary(binary.Path, binaryPath) { + return prepareLocalSource(binary, binaryPath, targetFolder) + } + _, err := os.Stat(binaryPath) + return binaryPath, err +} + func GetBinaries(context string, config *ProviderConfig) (map[string]string, error) { binariesDir, err := GetProviderBinariesDir(context, config.Name) if err != nil { @@ -132,26 +141,100 @@ func downloadBinaryForPlatform( continue } - // check if binary is correct - binaryTargetFolder := filepath.Join(targetFolder, strings.ToLower(binaryName)) - binaryPath := getBinaryPath(binary, binaryTargetFolder) - if verifyOrRemoveBinary(binaryPath, binary.Checksum) || - fromCache(binary, binaryTargetFolder) { - return binaryPath, nil - } - - // try to download the binary - binaryPath, err := downloadWithRetry(ctx, binaryName, binary, binaryTargetFolder) - if err != nil { - return "", err - } - return binaryPath, nil + return preparePlatformBinary(ctx, binaryName, binary, targetFolder) } return "", fmt.Errorf( "cannot download provider binary %s, because no binary location matched OS %s and ARCH %s", binaryName, runtime.GOOS, runtime.GOARCH) } +func preparePlatformBinary( + ctx context.Context, + binaryName string, + binary *ProviderBinary, + targetFolder string, +) (string, error) { + binaryTargetFolder := filepath.Join(targetFolder, strings.ToLower(binaryName)) + binaryPath := getBinaryPath(binary, binaryTargetFolder) + if isUnownedLocalBinary(binary.Path, binaryPath) { + return prepareLocalSource(binary, binaryPath, binaryTargetFolder) + } + if verifyOrRemoveBinary(binaryPath, binary.Checksum) || fromCache(binary, binaryTargetFolder) { + return binaryPath, nil + } + if err := verifyAvailableLocalSource(binary); err != nil { + return "", err + } + return downloadWithRetry(ctx, binaryName, binary, binaryTargetFolder) +} + +func verifyAvailableLocalSource(binary *ProviderBinary) error { + if isRemotePath(binary.Path) || filepath.IsAbs(binary.Path) { + return nil + } + _, err := verifyLocalBinary(binary.Path, binary.Checksum) + if errors.Is(err, os.ErrNotExist) { + return nil + } + return err +} + +func isUnownedLocalBinary(source, target string) bool { + return filepath.IsAbs(source) || sameLocalFile(source, target) +} + +func prepareLocalSource(binary *ProviderBinary, source, targetFolder string) (string, error) { + result, err := verifyLocalBinary(source, binary.Checksum) + if !filepath.IsAbs(binary.Path) || !errors.Is(err, os.ErrNotExist) { + return result, err + } + target := localTargetPath(binary, targetFolder) + absTarget, absErr := filepath.Abs(target) + if absErr != nil || filepath.Clean(absTarget) == filepath.Clean(source) { + return "", err + } + if _, targetErr := verifyLocalBinary(target, binary.Checksum); targetErr == nil { + return target, nil + } + return "", err +} + +func verifyLocalBinary(binaryPath, checksum string) (string, error) { + info, err := os.Stat(binaryPath) + if err != nil { + return "", fmt.Errorf("stat provider binary %s: %w", binaryPath, err) + } + if !info.Mode().IsRegular() { + return "", fmt.Errorf("provider binary %s is not a regular file", binaryPath) + } + if checksum == "" { + return binaryPath, nil + } + actual, err := hash.File(binaryPath) + if err != nil { + return "", fmt.Errorf("hash provider binary %s: %w", binaryPath, err) + } + if !strings.EqualFold(actual, checksum) { + return "", fmt.Errorf( + "provider binary %s SHA-256 mismatch: expected %s, actual %s; regenerate the manifest", + binaryPath, checksum, actual, + ) + } + return binaryPath, nil +} + +func sameLocalFile(source, target string) bool { + if isRemotePath(source) { + return false + } + sourceInfo, err := os.Stat(source) + if err != nil { + return false + } + targetInfo, err := os.Stat(target) + return err == nil && os.SameFile(sourceInfo, targetInfo) +} + func downloadWithRetry( ctx context.Context, binaryName string, @@ -350,6 +433,12 @@ func getBinaryFileName(binary *ProviderBinary) string { func getBinaryPath(binary *ProviderBinary, targetFolder string) string { if filepath.IsAbs(binary.Path) { + if _, err := os.Stat(binary.Path); errors.Is(err, os.ErrNotExist) { + target := localTargetPath(binary, targetFolder) + if _, targetErr := verifyLocalBinary(target, binary.Checksum); targetErr == nil { + return target + } + } return binary.Path } @@ -629,7 +718,7 @@ func downloadToTempFile(reader io.Reader) (string, error) { func localTargetPath(binary *ProviderBinary, targetFolder string) string { name := binary.Name if name == "" { - name = path.Base(binary.Path) + name = filepath.Base(binary.Path) } return filepath.Join(targetFolder, name) } diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go new file mode 100644 index 000000000..aa304279f --- /dev/null +++ b/pkg/provider/local_source_safety_test.go @@ -0,0 +1,446 @@ +package provider + +import ( + "context" + "crypto/sha256" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "sync/atomic" + "testing" + + "github.com/devsy-org/devsy/pkg/hash" + "github.com/stretchr/testify/suite" +) + +const fixtureBinaryName = "binary" + +type localSourceSafetySuite struct{ suite.Suite } + +func TestLocalSourceSafety(t *testing.T) { suite.Run(t, new(localSourceSafetySuite)) } + +func (s *localSourceSafetySuite) TestChecksumMismatch() { + for _, tc := range []struct { + name string + remote bool + }{{name: "absolute source"}, {name: "downloaded source", remote: true}} { + s.Run(tc.name, func() { + dir := s.T().TempDir() + source := filepath.Join(dir, "source") + contents := []byte("actual binary") + s.Require().NoError(os.WriteFile(source, contents, 0o600)) + path := source + var requests atomic.Int32 + if tc.remote { + server := httptest.NewServer( + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(contents) + }), + ) + defer server.Close() + path = server.URL + "/binary" + } + expected := strings.Repeat("0", 64) + actual := fmt.Sprintf("%x", sha256.Sum256(contents)) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: path, + Checksum: expected, + } + _, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + filepath.Join(dir, "storage"), + ) + s.Require().Error(err) + if tc.remote { + s.Positive(requests.Load()) + s.ErrorIs(err, errChecksumVerificationFailed) + s.NoFileExists(getBinaryPath(binary, filepath.Join(dir, "storage", "test"))) + } else { + s.FileExists(source) + got, readErr := hash.File(source) + s.Require().NoError(readErr) + s.Equal(actual, got) + s.ErrorContains(err, source) + s.ErrorContains(err, expected) + s.ErrorContains(err, actual) + s.ErrorContains(err, "regenerate") + } + }) + } +} + +func (s *localSourceSafetySuite) TestManifestPaths() { + dir := s.T().TempDir() + existing := filepath.Join(dir, "manifest") + s.Require().NoError(os.WriteFile(existing, []byte("name: test\n"), 0o600)) + for _, tc := range []struct { + name, path string + missing bool + }{ + {name: "missing yaml", path: filepath.Join(dir, "missing.yaml"), missing: true}, + {name: "missing yml", path: filepath.Join(dir, "missing.yml"), missing: true}, + {name: "missing absolute path", path: filepath.Join(dir, "missing"), missing: true}, + {name: "existing without extension", path: existing}, + } { + s.Run(tc.name, func() { + data, source, err := ResolveProvider(context.Background(), tc.path) + if tc.missing { + s.Require().Error(err) + s.ErrorContains(err, "provider manifest file not found: "+tc.path) + return + } + s.Require().NoError(err) + s.Equal("name: test\n", string(data)) + s.Equal(tc.path, source.File) + }) + } +} + +func (s *localSourceSafetySuite) TestRelativeSourceAtDestination() { + dir := s.T().TempDir() + s.Require().NoError(os.Mkdir(filepath.Join(dir, "test"), 0o750)) + source := filepath.Join(dir, "test", fixtureBinaryName) + s.Require().NoError(os.WriteFile(source, []byte("local"), 0o600)) + workingDir, err := os.Getwd() + s.Require().NoError(err) + relative, err := filepath.Rel(workingDir, source) + s.Require().NoError(err) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: relative, + Checksum: strings.Repeat("0", 64), + } + _, err = downloadBinaryForPlatform(context.Background(), "TEST", []*ProviderBinary{binary}, dir) + s.Require().ErrorContains(err, "regenerate") + s.FileExists(source) + got, readErr := hash.File(source) + s.Require().NoError(readErr) + s.Equal(fmt.Sprintf("%x", sha256.Sum256([]byte("local"))), got) + binary.Checksum = fmt.Sprintf("%x", sha256.Sum256([]byte("expected"))) + config := &ProviderConfig{Binaries: map[string][]*ProviderBinary{"TEST": {binary}}} + _, err = GetBinariesFrom(config, dir) + s.Require().ErrorContains(err, "SHA-256 mismatch") + s.FileExists(source) +} + +func (s *localSourceSafetySuite) TestInvalidCachedBinaryIsRemoved() { + file := filepath.Join(s.T().TempDir(), "cached") + s.Require().NoError(os.WriteFile(file, []byte("stale"), 0o600)) + s.False(verifyOrRemoveBinary(file, strings.Repeat("0", 64))) + s.NoFileExists(file) +} + +func (s *localSourceSafetySuite) TestGitHubSourcesDoNotResolveAsFiles() { + for _, source := range []string{"owner/repo", "owner/repo.yaml", "github.com/owner/repo.yml"} { + s.Run(source, func() { + _, ok, err := resolveFileProvider(source, &ProviderSource{}) + s.NoError(err) + s.False(ok) + }) + } +} + +func (s *localSourceSafetySuite) TestMissingAbsoluteSourceUsesStoredCopy() { + dir := s.T().TempDir() + target := filepath.Join(dir, "test", fixtureBinaryName) + s.Require().NoError(os.MkdirAll(filepath.Dir(target), 0o750)) + content := []byte("installed binary") + s.Require().NoError(os.WriteFile(target, content, 0o600)) + checksum := fmt.Sprintf("%x", sha256.Sum256(content)) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: filepath.Join(dir, "missing"), + Name: fixtureBinaryName, + Checksum: checksum, + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(target, got) + s.FileExists(target) + config := &ProviderConfig{Binaries: map[string][]*ProviderBinary{"TEST": {binary}}} + binaries, err := GetBinariesFrom(config, dir) + s.Require().NoError(err) + s.Equal(target, binaries["TEST"]) + s.Require().NoError(os.WriteFile(binary.Path, []byte("changed source"), 0o600)) + _, err = GetBinariesFrom(config, dir) + s.Require().ErrorContains(err, "SHA-256 mismatch") + s.FileExists(binary.Path) + s.Require().NoError(os.Remove(binary.Path)) + s.Require().NoError(os.WriteFile(target, []byte("changed"), 0o600)) + _, err = GetBinariesFrom(config, dir) + s.Require().Error(err) + s.FileExists(target) +} + +func (s *localSourceSafetySuite) TestMissingExplicitManifestForms() { + name := filepath.Base(s.T().TempDir()) + ".yaml" + home, err := os.UserHomeDir() + s.Require().NoError(err) + cases := []struct { + name, source, want string + local bool + }{ + { + name: "dot", + source: "./" + name, + want: filepath.Join(mustWorkingDir(s), name), + local: true, + }, + { + name: "parent", + source: "../" + name, + want: filepath.Join(filepath.Dir(mustWorkingDir(s)), name), + local: true, + }, + { + name: "absolute", + source: filepath.Join(s.T().TempDir(), name), + want: filepath.Join(s.T().TempDir(), name), + local: true, + }, + {name: "home", source: "~/" + name, want: filepath.Join(home, name), local: true}, + {name: "bare repository", source: "owner/repo.yaml"}, + {name: "bare yaml", source: name}, + } + cases[2].want = cases[2].source + if runtime.GOOS == windowsOS { + drivePath := filepath.Join(filepath.VolumeName(mustWorkingDir(s))+`\`, name) + cases = append(cases, struct { + name, source, want string + local bool + }{name: "backslash", source: `.\` + name, want: filepath.Join(mustWorkingDir(s), name), local: true}) + cases = append(cases, struct { + name, source, want string + local bool + }{name: "drive", source: drivePath, want: drivePath, local: true}) + } + for _, tc := range cases { + s.Run(tc.name, func() { + _, ok, err := resolveFileProvider(tc.source, &ProviderSource{}) + if !tc.local { + s.NoError(err) + s.False(ok) + return + } + s.True(ok) + s.Require().Error(err) + s.ErrorContains(err, "provider manifest file not found: "+tc.want) + }) + } +} + +func mustWorkingDir(s *localSourceSafetySuite) string { + dir, err := os.Getwd() + s.Require().NoError(err) + return dir +} + +func (s *localSourceSafetySuite) TestExplicitNonFileAndHomePrefix() { + dir := s.T().TempDir() + _, ok, err := resolveFileProvider(dir, &ProviderSource{}) + s.True(ok) + s.ErrorContains(err, "not a regular file") + path, err := expandManifestPath("~providers/provider.yaml") + s.NoError(err) + s.Equal("~providers/provider.yaml", path) +} + +func (s *localSourceSafetySuite) TestValidStoredCopySurvivesChangedRelativeSource() { + dir := s.T().TempDir() + source := filepath.Join(dir, "source") + stored := filepath.Join(dir, "storage", "test", fixtureBinaryName) + s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) + s.Require().NoError(os.WriteFile(source, []byte("changed"), 0o600)) + content := []byte("installed") + s.Require().NoError(os.WriteFile(stored, content, 0o600)) + cwd, err := os.Getwd() + s.Require().NoError(err) + relative, err := filepath.Rel(cwd, source) + s.Require().NoError(err) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: relative, + Name: fixtureBinaryName, + Checksum: fmt.Sprintf("%x", sha256.Sum256(content)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + filepath.Join(dir, "storage"), + ) + s.Require().NoError(err) + s.Equal(stored, got) + s.FileExists(source) +} + +func (s *localSourceSafetySuite) TestDrivePathClassification() { + cases := []struct { + path string + want bool + }{ + {path: "x:foo", want: runtime.GOOS == windowsOS}, + {path: "C:/missing.yaml", want: true}, + {path: `C:\missing.yaml`, want: true}, + {path: "1:foo", want: false}, + } + for _, tc := range cases { + s.Run(tc.path, func() { s.Equal(tc.want, isExplicitManifestPath(tc.path)) }) + } +} + +func (s *localSourceSafetySuite) TestMissingSourceFallbackUsesLocalFilename() { + dir := s.T().TempDir() + source := filepath.Join(dir, "source", fixtureBinaryName) + stored := filepath.Join(dir, "test", fixtureBinaryName) + s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) + contents := []byte("installed") + s.Require().NoError(os.WriteFile(stored, contents, 0o600)) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: source, + Checksum: fmt.Sprintf("%x", sha256.Sum256(contents)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(stored, got) +} + +func (s *localSourceSafetySuite) TestValidLocalSources() { + for _, relative := range []bool{false, true} { + s.Run(fmt.Sprintf("relative=%t", relative), func() { + dir := s.T().TempDir() + source := filepath.Join(dir, "test", fixtureBinaryName) + s.Require().NoError(os.MkdirAll(filepath.Dir(source), 0o750)) + contents := []byte("valid binary") + s.Require().NoError(os.WriteFile(source, contents, 0o600)) + path := source + if relative { + cwd, err := os.Getwd() + s.Require().NoError(err) + path, err = filepath.Rel(cwd, source) + s.Require().NoError(err) + } + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: path, + Checksum: fmt.Sprintf("%x", sha256.Sum256(contents)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(source, got) + }) + } +} + +func (s *localSourceSafetySuite) TestHomeManifestSymlinkTraversal() { + if runtime.GOOS == windowsOS { + s.T().Skip("symlinks may require elevated privileges on Windows") + } + home := s.T().TempDir() + s.T().Setenv("HOME", home) + outer := s.T().TempDir() + s.Require().NoError(os.Mkdir(filepath.Join(outer, "nested"), 0o750)) + s.Require().NoError(os.Symlink(filepath.Join(outer, "nested"), filepath.Join(home, "alias"))) + expected := []byte("name: from-outer\n") + s.Require().NoError(os.WriteFile(filepath.Join(outer, "provider.yaml"), expected, 0o600)) + s.Require(). + NoError(os.WriteFile(filepath.Join(home, "provider.yaml"), []byte("name: from-home\n"), 0o600)) + source := &ProviderSource{} + data, ok, err := resolveFileProvider("~/alias/../provider.yaml", source) + s.Require().NoError(err) + s.True(ok) + s.Equal(expected, data) + reloaded, err := hash.File(source.File) + s.Require().NoError(err) + s.Equal(fmt.Sprintf("%x", sha256.Sum256(expected)), reloaded) + resolvedOuter, err := filepath.EvalSymlinks(outer) + s.Require().NoError(err) + s.Equal(resolvedOuter, filepath.Dir(source.File)) +} + +func (s *localSourceSafetySuite) TestRelativeParentTraversalDoesNotAliasTarget() { + if runtime.GOOS == windowsOS { + s.T().Skip("symlinks may require elevated privileges on Windows") + } + dir := s.T().TempDir() + folder := filepath.Join(dir, "test") + outside := s.T().TempDir() + s.Require().NoError(os.MkdirAll(filepath.Join(outside, "nested"), 0o750)) + s.Require().NoError(os.MkdirAll(folder, 0o750)) + s.Require(). + NoError(os.Symlink(filepath.Join(outside, "nested"), filepath.Join(folder, "alias"))) + content := []byte("correct source") + s.Require().NoError(os.WriteFile(filepath.Join(outside, fixtureBinaryName), content, 0o600)) + target := filepath.Join(folder, fixtureBinaryName) + s.Require().NoError(os.WriteFile(target, []byte("stale target"), 0o600)) + cwd, err := os.Getwd() + s.Require().NoError(err) + alias, err := filepath.Rel(cwd, filepath.Join(folder, "alias")) + s.Require().NoError(err) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: alias + "/../binary", + Name: fixtureBinaryName, + Checksum: fmt.Sprintf("%x", sha256.Sum256(content)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(target, got) + s.FileExists(filepath.Join(outside, fixtureBinaryName)) +} + +func (s *localSourceSafetySuite) TestMissingExternalRuntimeUsesVerifiedStoredCopy() { + dir := s.T().TempDir() + agent := externalAgentFixture() + binary := agent.Binaries["RUNTIME"][0] + binary.Path = filepath.Join(dir, "missing") + binary.Name = "runtime" + stored := filepath.Join(dir, "runtime", "runtime") + s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) + fixture := &externalConfigSuite{} + fixture.SetT(s.T()) + fixture.writeExecutable(stored, externalFixturePayload) + resolved, err := ResolveExternalRuntimeBinary(agent, dir) + s.Require().NoError(err) + s.Equal(stored, resolved) + s.Require().NoError(os.WriteFile(stored, []byte("tampered"), 0o600)) + _, err = ResolveExternalRuntimeBinary(agent, dir) + s.Require().Error(err) + s.FileExists(stored) +} diff --git a/pkg/provider/resolve.go b/pkg/provider/resolve.go index a740db405..b9362738d 100644 --- a/pkg/provider/resolve.go +++ b/pkg/provider/resolve.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path/filepath" + "runtime" "strings" "github.com/devsy-org/devsy/pkg/config" @@ -195,24 +196,32 @@ func resolveFileProvider( providerSource string, retSource *ProviderSource, ) ([]byte, bool, error) { - if !strings.HasSuffix(providerSource, ".yaml") && !strings.HasSuffix(providerSource, ".yml") { + explicit := isExplicitManifestPath(providerSource) + if skipManifestSource(providerSource, explicit) { return nil, false, nil } - - if _, err := os.Stat(providerSource); err != nil { - if os.IsNotExist(err) { - return nil, false, nil + filePath, err := expandManifestPath(providerSource) + if err != nil { + return nil, true, err + } + info, err := os.Stat(filePath) + if err != nil { + return resolveFileStatError(filePath, explicit, err) + } + if !info.Mode().IsRegular() { + if explicit { + return nil, true, fmt.Errorf("provider manifest is not a regular file: %s", filePath) } - return nil, true, fmt.Errorf("stat provider file %q: %w", providerSource, err) + return nil, false, nil } // #nosec G304 - providerSource is user-provided path for loading provider config - out, err := os.ReadFile(providerSource) + out, err := os.ReadFile(filePath) if err != nil { return nil, true, fmt.Errorf("read provider file %q: %w", providerSource, err) } - absPath, err := filepath.Abs(providerSource) + absPath, err := absoluteManifestPath(filePath) if err != nil { return nil, true, fmt.Errorf("resolve absolute path for %q: %w", providerSource, err) } @@ -220,6 +229,78 @@ func resolveFileProvider( return out, true, nil } +func absoluteManifestPath(filePath string) (string, error) { + for part := range strings.SplitSeq(filepath.ToSlash(filePath), "/") { + if part == ".." { + resolved, err := filepath.EvalSymlinks(filePath) + if err != nil { + return "", err + } + return filepath.Abs(resolved) + } + } + return filepath.Abs(filePath) +} + +func skipManifestSource(source string, explicit bool) bool { + return !explicit && !hasYAMLSuffix(source) +} + +func isExplicitManifestPath(source string) bool { + return filepath.IsAbs(source) || strings.HasPrefix(source, "/") || + strings.HasPrefix(source, "./") || + strings.HasPrefix(source, "../") || + strings.HasPrefix(source, "~") || + strings.Contains(source, `\`) || + isWindowsDrivePath(source) +} + +func isWindowsDrivePath(source string) bool { + if len(source) < 2 || source[1] != ':' { + return false + } + letter := source[0] | 0x20 + return letter >= 'a' && letter <= 'z' && + (runtime.GOOS == windowsOS || (len(source) > 2 && source[2] == '/')) +} + +func expandManifestPath(source string) (string, error) { + if source != "~" && !strings.HasPrefix(source, "~/") && !strings.HasPrefix(source, `~\`) { + return source, nil + } + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("resolve home directory: %w", err) + } + return strings.TrimRight( + home, + `/\`, + ) + string( + filepath.Separator, + ) + strings.TrimLeft( + source[1:], + `/\`, + ), nil +} + +func hasYAMLSuffix(source string) bool { + return strings.HasSuffix(source, ".yaml") || strings.HasSuffix(source, ".yml") +} + +func resolveFileStatError(source string, manifestPath bool, err error) ([]byte, bool, error) { + if !os.IsNotExist(err) { + return nil, true, fmt.Errorf("stat provider file %q: %w", source, err) + } + if !manifestPath { + return nil, false, nil + } + absPath, absErr := filepath.Abs(source) + if absErr != nil { + return nil, true, fmt.Errorf("resolve absolute path for %q: %w", source, absErr) + } + return nil, true, fmt.Errorf("provider manifest file not found: %s", absPath) +} + func downloadProvider(ctx context.Context, url string) ([]byte, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil {