@@ -59,7 +59,7 @@ type Filesystem struct {
59
59
// files, gid ownership of the volume's data directory will be changed to
60
60
// the value. Attribute value must be a valid int64 value.
61
61
// If FixedFSGroup is defined, this field has no effect.
62
- FSGroupVolumeAttributeKey * string
62
+ FSGroupVolumeAttributeKey string
63
63
}
64
64
65
65
// Ensure the Filesystem implementation is fully featured
@@ -194,7 +194,7 @@ func (f *Filesystem) RegisterMetadata(meta metadata.Metadata) (bool, error) {
194
194
// to a custom gid.
195
195
func (f * Filesystem ) WriteFiles (meta metadata.Metadata , files map [string ][]byte ) error {
196
196
// Data directory should be read, write and execute only to the fs user; read and executable to group
197
- if err := os .MkdirAll (f .dataPathForVolumeID (meta .VolumeID ), 0750 ); err != nil {
197
+ if err := os .MkdirAll (f .dataPathForVolumeID (meta .VolumeID ), 0550 ); err != nil {
198
198
return err
199
199
}
200
200
@@ -215,8 +215,28 @@ func (f *Filesystem) WriteFiles(meta metadata.Metadata, files map[string][]byte)
215
215
return err
216
216
}
217
217
218
- payload := makePayload (files , fsGroup )
219
- return writer .Write (payload )
218
+ payload := makePayload (files )
219
+ if err := writer .Write (payload ); err != nil {
220
+ return err
221
+ }
222
+
223
+ // If a fsGroup is defined, Chown all files within the data directory.
224
+ if fsGroup != nil {
225
+ dirName := f .dataPathForVolumeID (meta .VolumeID )
226
+ entries , err := os .ReadDir (dirName )
227
+ if err != nil {
228
+ return fmt .Errorf ("failed to list files in data directory: %w" , err )
229
+ }
230
+
231
+ for _ , entry := range entries {
232
+ // Set the uid to -1 which means don't change ownership in Go.
233
+ if err := os .Chown (filepath .Join (dirName , entry .Name ()), - 1 , int (* fsGroup )); err != nil {
234
+ return err
235
+ }
236
+ }
237
+ }
238
+
239
+ return nil
220
240
}
221
241
222
242
// ReadFile reads the named file within the volume's data directory.
@@ -252,13 +272,12 @@ func (f *Filesystem) tempfsPath() string {
252
272
return filepath .Join (f .baseDir , "inmemfs" )
253
273
}
254
274
255
- func makePayload (in map [string ][]byte , fsGroup * int64 ) map [string ]util.FileProjection {
275
+ func makePayload (in map [string ][]byte ) map [string ]util.FileProjection {
256
276
out := make (map [string ]util.FileProjection , len (in ))
257
277
for name , data := range in {
258
278
out [name ] = util.FileProjection {
259
- Data : data ,
260
- FsGroup : fsGroup ,
261
- Mode : readOnlyUserAndGroupFileMode ,
279
+ Data : data ,
280
+ Mode : readOnlyUserAndGroupFileMode ,
262
281
}
263
282
}
264
283
return out
@@ -274,19 +293,27 @@ func (f *Filesystem) fsGroupForMetadata(meta metadata.Metadata) (*int64, error)
274
293
}
275
294
276
295
// If the FSGroupVolumeAttributeKey is not defined, no ownership can change.
277
- if f .FSGroupVolumeAttributeKey == nil {
296
+ if len ( f .FSGroupVolumeAttributeKey ) == 0 {
278
297
return nil , nil
279
298
}
280
299
281
- fsGroupStr , ok := meta .VolumeContext [* f .FSGroupVolumeAttributeKey ]
300
+ fsGroupStr , ok := meta .VolumeContext [f .FSGroupVolumeAttributeKey ]
282
301
if ! ok {
283
302
// If the attribute has not been set, return no ownership change.
284
303
return nil , nil
285
304
}
286
305
287
306
fsGroup , err := strconv .ParseInt (fsGroupStr , 10 , 64 )
288
307
if err != nil {
289
- return nil , fmt .Errorf ("failed to parse %q, value must be a valid integer: %w" , * f .FSGroupVolumeAttributeKey , err )
308
+ return nil , fmt .Errorf ("failed to parse %q, value must be a valid integer: %w" , f .FSGroupVolumeAttributeKey , err )
309
+ }
310
+
311
+ // fsGroup has to be between 1 and 4294967295 inclusive. 4294967295 is the
312
+ // largest gid number on most modern operating systems. If the actual maximum
313
+ // is smaller on the running machine, then we will simply error later during
314
+ // the Chmod.
315
+ if fsGroup <= 0 || fsGroup > 4294967295 {
316
+ return nil , fmt .Errorf ("%q: gid value must be greater than 0 and less than 4294967295: %d" , f .FSGroupVolumeAttributeKey , fsGroup )
290
317
}
291
318
292
319
return & fsGroup , nil
0 commit comments