B: static vars should only resolve on "" sources

Signed-off-by: Rui Yang <ruiya@vmware.com>
Co-authored-by: Bohan Chen <bochen@pivotal.io>
This commit is contained in:
Rui Yang 2021-03-01 11:22:45 -05:00
parent 81c39e21d4
commit c1012fb8b8
6 changed files with 53 additions and 2 deletions

View File

@ -32,7 +32,7 @@ func newBuildVariables(credVars vars.Variables, enableRedaction bool) *buildVari
func (b *buildVariables) Get(ref vars.Reference) (interface{}, bool, error) {
if ref.Source == "." {
b.lock.RLock()
val, found, err := b.localVars.Get(ref)
val, found, err := b.localVars.Get(ref.WithoutSource())
b.lock.RUnlock()
if found || err != nil {
return val, found, err

View File

@ -12,7 +12,7 @@ func (m NamedVariables) Get(ref Reference) (interface{}, bool, error) {
}
if vars, ok := m[ref.Source]; ok {
return vars.Get(ref)
return vars.Get(ref.WithoutSource())
}
return nil, false, MissingSourceError{Name: ref.String(), Source: ref.Source}

View File

@ -5,6 +5,10 @@ type StaticVariables map[string]interface{}
var _ Variables = StaticVariables{}
func (v StaticVariables) Get(ref Reference) (interface{}, bool, error) {
if ref.Source != "" {
return nil, false, nil
}
val, found := v[ref.Path]
if !found {
return nil, false, nil

View File

@ -27,6 +27,24 @@ var _ = Describe("StaticVariables", func() {
Expect(err).ToNot(HaveOccurred())
})
It("returns nil and not found if source is from local vars", func() {
a := StaticVariables{"a": "foo"}
val, found, err := a.Get(Reference{Source: ".", Path: "a"})
Expect(val).To(BeNil())
Expect(found).To(BeFalse())
Expect(err).ToNot(HaveOccurred())
})
It("returns nil and not found if source is from var source", func() {
a := StaticVariables{"a": "foo"}
val, found, err := a.Get(Reference{Source: "some-var-source", Path: "a"})
Expect(val).To(BeNil())
Expect(found).To(BeFalse())
Expect(err).ToNot(HaveOccurred())
})
It("follows fields", func() {
v := StaticVariables{
"a": map[string]interface{}{

View File

@ -270,6 +270,28 @@ jobs:
Expect(result).To(Equal([]byte(`"foo"="foo"`)))
})
It("ignores values referencing local var sources", func() {
byteSlice := []byte("((key))=((.:key))")
variables := vars.StaticVariables{
"key": "foo",
}
result, err := vars.NewTemplateResolver(byteSlice, []vars.Variables{variables}).Resolve(false, true)
Expect(err).NotTo(HaveOccurred())
Expect(string(result)).To(Equal("foo=((.:key))\n"))
})
It("ignores values referencing var sources", func() {
byteSlice := []byte("((key))=((source:key))")
variables := vars.StaticVariables{
"key": "foo",
}
result, err := vars.NewTemplateResolver(byteSlice, []vars.Variables{variables}).Resolve(false, true)
Expect(err).NotTo(HaveOccurred())
Expect(string(result)).To(Equal("foo=((source:key))\n"))
})
It("can template values with strange newlines", func() {
byteSlice := []byte("{{key}}")
variables := vars.StaticVariables{

View File

@ -18,6 +18,13 @@ type Reference struct {
Fields []string
}
func (r Reference) WithoutSource() Reference {
return Reference{
Path: r.Path,
Fields: r.Fields,
}
}
func ParseReference(name string) (Reference, error) {
var ref Reference