-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21765 from Luap99/machine-pull-policy
pkg/machine: add custom policy.json logic
- Loading branch information
Showing
8 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ocipull | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// DefaultPolicyJSONPath should be overwritten at build time with the real path to the directory where | ||
// the shipped policy.json file is located. This can either be absolute path or a relative path. If it | ||
// is relative it will be resolved relative to the podman binary and NOT the CWD. | ||
// | ||
// use "-X github.com/containers/podman/v5/pkg/machine/ocipull.DefaultPolicyJSONPath=/somepath" in go ldflags to overwrite this | ||
var DefaultPolicyJSONPath = "" | ||
|
||
const policyfile = "policy.json" | ||
|
||
type defaultPolicyError struct { | ||
errs []error | ||
} | ||
|
||
func (e *defaultPolicyError) Error() string { | ||
return fmt.Sprintf("no DefaultPolicyJSONPath defined and no local overwrites found: %q", e.errs) | ||
} | ||
|
||
func policyPath() (string, error) { | ||
paths := localPolicyOverwrites() | ||
errs := make([]error, 0, len(paths)) | ||
for _, path := range paths { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return path, nil | ||
} | ||
errs = append(errs, err) | ||
} | ||
if DefaultPolicyJSONPath != "" { | ||
if filepath.IsAbs(DefaultPolicyJSONPath) { | ||
return filepath.Join(DefaultPolicyJSONPath, policyfile), nil | ||
} | ||
p, err := os.Executable() | ||
if err != nil { | ||
return "", fmt.Errorf("could not resolve relative path to binary: %w", err) | ||
} | ||
return filepath.Join(p, DefaultPolicyJSONPath, policyfile), nil | ||
} | ||
return "", &defaultPolicyError{errs: errs} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"default": [ | ||
{ | ||
"type": "insecureAcceptAnything" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build !windows | ||
|
||
package ocipull | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/containers/common/pkg/config" | ||
"github.com/containers/storage/pkg/homedir" | ||
) | ||
|
||
func localPolicyOverwrites() []string { | ||
var dirs []string | ||
if p, err := homedir.GetConfigHome(); err == nil { | ||
dirs = append(dirs, filepath.Join(p, "containers", policyfile)) | ||
} | ||
dirs = append(dirs, config.DefaultSignaturePolicyPath) | ||
return dirs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ocipull | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func localPolicyOverwrites() []string { | ||
return []string{filepath.Join(os.Getenv("APPDATA"), "containers", policyfile)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters