@@ -1911,6 +1911,85 @@ func TestLoadAndActivateBundlesFromDisk(t *testing.T) {
1911
1911
}
1912
1912
}
1913
1913
1914
+ func TestLoadAndActivateBundlesFromDiskReservedChars (t * testing.T ) {
1915
+
1916
+ ctx := context .Background ()
1917
+ manager := getTestManager ()
1918
+
1919
+ dir := t .TempDir ()
1920
+
1921
+ goos = "windows"
1922
+
1923
+ bundleName := "test?bundle=opa" // bundle name contains reserved characters
1924
+ bundleSource := Source {
1925
+ Persist : true ,
1926
+ }
1927
+
1928
+ bundles := map [string ]* Source {}
1929
+ bundles [bundleName ] = & bundleSource
1930
+
1931
+ plugin := New (& Config {Bundles : bundles }, manager )
1932
+ plugin .bundlePersistPath = filepath .Join (dir , ".opa" )
1933
+
1934
+ plugin .loadAndActivateBundlesFromDisk (ctx )
1935
+
1936
+ // persist a bundle to disk and then load it
1937
+ module := "package foo\n \n corge=1"
1938
+
1939
+ b := bundle.Bundle {
1940
+ Manifest : bundle.Manifest {Revision : "quickbrownfaux" },
1941
+ Data : util .MustUnmarshalJSON ([]byte (`{"foo": {"bar": 1, "baz": "qux"}}` )).(map [string ]interface {}),
1942
+ Modules : []bundle.ModuleFile {
1943
+ {
1944
+ URL : "/foo/bar.rego" ,
1945
+ Path : "/foo/bar.rego" ,
1946
+ Parsed : ast .MustParseModule (module ),
1947
+ Raw : []byte (module ),
1948
+ },
1949
+ },
1950
+ }
1951
+
1952
+ b .Manifest .Init ()
1953
+
1954
+ var buf bytes.Buffer
1955
+ if err := bundle .NewWriter (& buf ).UseModulePath (true ).Write (b ); err != nil {
1956
+ t .Fatal ("unexpected error:" , err )
1957
+ }
1958
+
1959
+ err := plugin .saveBundleToDisk (bundleName , & buf )
1960
+ if err != nil {
1961
+ t .Fatalf ("unexpected error %v" , err )
1962
+ }
1963
+
1964
+ plugin .loadAndActivateBundlesFromDisk (ctx )
1965
+
1966
+ txn := storage .NewTransactionOrDie (ctx , manager .Store )
1967
+ defer manager .Store .Abort (ctx , txn )
1968
+
1969
+ ids , err := manager .Store .ListPolicies (ctx , txn )
1970
+ if err != nil {
1971
+ t .Fatal (err )
1972
+ } else if len (ids ) != 1 {
1973
+ t .Fatal ("Expected 1 policy" )
1974
+ }
1975
+
1976
+ bs , err := manager .Store .GetPolicy (ctx , txn , ids [0 ])
1977
+ exp := []byte ("package foo\n \n corge=1" )
1978
+ if err != nil {
1979
+ t .Fatal (err )
1980
+ } else if ! bytes .Equal (bs , exp ) {
1981
+ t .Fatalf ("Bad policy content. Exp:\n %v\n \n Got:\n \n %v" , string (exp ), string (bs ))
1982
+ }
1983
+
1984
+ data , err := manager .Store .Read (ctx , txn , storage.Path {})
1985
+ expData := util .MustUnmarshalJSON ([]byte (`{"foo": {"bar": 1, "baz": "qux"}, "system": {"bundles": {"test?bundle=opa": {"etag": "", "manifest": {"revision": "quickbrownfaux", "roots": [""]}}}}}` ))
1986
+ if err != nil {
1987
+ t .Fatal (err )
1988
+ } else if ! reflect .DeepEqual (data , expData ) {
1989
+ t .Fatalf ("Bad data content. Exp:\n %v\n \n Got:\n \n %v" , expData , data )
1990
+ }
1991
+ }
1992
+
1914
1993
func TestLoadAndActivateBundlesFromDiskV1Compatible (t * testing.T ) {
1915
1994
type update struct {
1916
1995
modules map [string ]string
@@ -6089,6 +6168,58 @@ func TestPluginManualTriggerWithTimeout(t *testing.T) {
6089
6168
}
6090
6169
}
6091
6170
6171
+ func TestGetNormalizedBundleName (t * testing.T ) {
6172
+ cases := []struct {
6173
+ input string
6174
+ goos string
6175
+ exp string
6176
+ }{
6177
+ {
6178
+ input : "foo" ,
6179
+ exp : "foo" ,
6180
+ },
6181
+ {
6182
+ input : "foo=bar" ,
6183
+ exp : "foo=bar" ,
6184
+ goos : "windows" ,
6185
+ },
6186
+ {
6187
+ input : "c:/foo" ,
6188
+ exp : "c:/foo" ,
6189
+ },
6190
+ {
6191
+ input : "c:/foo" ,
6192
+ exp : "c\\ :\\ /foo" ,
6193
+ goos : "windows" ,
6194
+ },
6195
+ {
6196
+ input : "file:\" <>c:/a" ,
6197
+ exp : "file\\ :\\ \" \\ <\\ >c\\ :\\ /a" ,
6198
+ goos : "windows" ,
6199
+ },
6200
+ {
6201
+ input : "|a?b*c" ,
6202
+ exp : "\\ |a\\ ?b\\ *c" ,
6203
+ goos : "windows" ,
6204
+ },
6205
+ {
6206
+ input : "a?b=c" ,
6207
+ exp : "a\\ ?b=c" ,
6208
+ goos : "windows" ,
6209
+ },
6210
+ }
6211
+
6212
+ for _ , tc := range cases {
6213
+ t .Run (tc .input , func (t * testing.T ) {
6214
+ goos = tc .goos
6215
+ actual := getNormalizedBundleName (tc .input )
6216
+ if actual != tc .exp {
6217
+ t .Fatalf ("Want %v but got: %v" , tc .exp , actual )
6218
+ }
6219
+ })
6220
+ }
6221
+ }
6222
+
6092
6223
func writeTestBundleToDisk (t * testing.T , srcDir string , signed bool ) bundle.Bundle {
6093
6224
t .Helper ()
6094
6225
0 commit comments