-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support getting package versions from external files (#3363)
* feat: support getting package versions from external files * chore(go): go mod tidy * fix: fix a lint error * fix: restrict the value of version_expr for security * feat: support version_expr_prefix --------- Co-authored-by: aquaproj-aqua-releaser[bot] <95135029+aquaproj-aqua-releaser[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0425338
commit 2933920
Showing
13 changed files
with
194 additions
and
14 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
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,94 @@ | ||
package expr | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/expr-lang/expr" | ||
"github.com/spf13/afero" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Reader struct { | ||
pwd string | ||
fs afero.Fs | ||
} | ||
|
||
const safeVersionPattern = `^v?\d+\.\d+(\.\d+)*[.-]?((alpha|beta|dev|rc)[.-]?)?\d*` | ||
|
||
var safeVersionRegexp = regexp.MustCompile(safeVersionPattern) | ||
|
||
func EvalVersionExpr(fs afero.Fs, pwd string, expression string) (string, error) { | ||
r := Reader{fs: fs, pwd: pwd} | ||
compiled, err := expr.Compile(expression, expr.Env(map[string]any{ | ||
"readFile": r.readFile, | ||
"readJSON": r.readJSON, | ||
"readYAML": r.readYAML, | ||
})) | ||
if err != nil { | ||
return "", fmt.Errorf("parse the expression: %w", err) | ||
} | ||
a, err := expr.Run(compiled, map[string]any{ | ||
"readFile": r.readFile, | ||
"readJSON": r.readJSON, | ||
"readYAML": r.readYAML, | ||
}) | ||
if err != nil { | ||
// Don't output error to prevent leaking sensitive information | ||
// Maybe malicious users tries to read a secret file | ||
return "", errors.New("evaluate the expression") | ||
} | ||
s, ok := a.(string) | ||
if !ok { | ||
return "", errMustBeString | ||
} | ||
// Restrict the value of version_expr to a semver for security reason. | ||
// This prevents secrets from being exposed. | ||
if !safeVersionRegexp.MatchString(s) { | ||
// Don't output the valuof of version_expr to prevent leaking sensitive information | ||
// Maybe malicious users tries to read a secret file | ||
return "", errors.New("the evaluation result of version_expr must match with " + safeVersionPattern) | ||
} | ||
return s, nil | ||
} | ||
|
||
func (r *Reader) read(s string) []byte { | ||
if !filepath.IsAbs(s) { | ||
s = filepath.Join(r.pwd, s) | ||
} | ||
b, err := afero.ReadFile(r.fs, s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} | ||
|
||
func (r *Reader) readFile(s string) string { | ||
return strings.TrimSpace(string(r.read(s))) | ||
} | ||
|
||
func (r *Reader) readJSON(s string) any { | ||
b := r.read(s) | ||
var a any | ||
if err := json.Unmarshal(b, &a); err != nil { | ||
// Don't output error to prevent leaking sensitive information | ||
// Maybe malicious users tries to read a secret file | ||
panic("failed to unmarshal JSON") | ||
} | ||
return a | ||
} | ||
|
||
func (r *Reader) readYAML(s string) any { | ||
b := r.read(s) | ||
var a any | ||
if err := yaml.Unmarshal(b, &a); err != nil { | ||
// Don't output error to prevent leaking sensitive information | ||
// Maybe malicious users tries to read a secret file | ||
panic("failed to unmarshal YAML") | ||
} | ||
return a | ||
} |
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 @@ | ||
1.10.2 |
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,18 @@ | ||
--- | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json | ||
# aqua - Declarative CLI Version Manager | ||
# https://aquaproj.github.io/ | ||
# checksum: | ||
# enabled: true | ||
# require_checksum: true | ||
# supported_envs: | ||
# - all | ||
registries: | ||
- type: standard | ||
ref: v4.276.0 # renovate: depName=aquaproj/aqua-registry | ||
packages: | ||
- name: hashicorp/terraform | ||
version_expr: | | ||
"v" + readFile('.terraform-version') | ||
# version_template: v{{readFile '.terraform-version'}} | ||
# version_template: v{{(readYAML 'foo.yaml').version}} |
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,16 @@ | ||
--- | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json | ||
# aqua - Declarative CLI Version Manager | ||
# https://aquaproj.github.io/ | ||
# checksum: | ||
# enabled: true | ||
# require_checksum: true | ||
# supported_envs: | ||
# - all | ||
registries: | ||
- type: standard | ||
ref: v4.276.0 # renovate: depName=aquaproj/aqua-registry | ||
packages: | ||
- name: hashicorp/terraform | ||
version_expr: | | ||
readJSON('version.json').version |
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,3 @@ | ||
{ | ||
"version": "1.10.1" | ||
} |
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,16 @@ | ||
--- | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json | ||
# aqua - Declarative CLI Version Manager | ||
# https://aquaproj.github.io/ | ||
# checksum: | ||
# enabled: true | ||
# require_checksum: true | ||
# supported_envs: | ||
# - all | ||
registries: | ||
- type: standard | ||
ref: v4.276.0 # renovate: depName=aquaproj/aqua-registry | ||
packages: | ||
- name: hashicorp/terraform | ||
version_expr: | | ||
readYAML('version.yaml').version |
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 @@ | ||
version: 1.10.1 |