@@ -51,6 +51,9 @@ type KubeVolume struct {
5151// If the volume is optional, we can move on if it is not found
5252// Only used when there are volumes in a yaml that refer to a configmap
5353Optional bool
54+ // DefaultMode sets the permissions on files created for the volume
55+ // This is optional and defaults to 0644
56+ DefaultMode int32
5457}
5558
5659// Create a KubeVolume from an HostPathVolumeSource
@@ -135,9 +138,18 @@ func VolumeFromHostPath(hostPath *v1.HostPathVolumeSource, mountLabel string) (*
135138// VolumeFromSecret creates a new kube volume from a kube secret.
136139func VolumeFromSecret (secretSource * v1.SecretVolumeSource , secretsManager * secrets.SecretsManager ) (* KubeVolume , error ) {
137140kv := & KubeVolume {
138- Type : KubeVolumeTypeSecret ,
139- Source : secretSource .SecretName ,
140- Items : map [string ][]byte {},
141+ Type : KubeVolumeTypeSecret ,
142+ Source : secretSource .SecretName ,
143+ Items : map [string ][]byte {},
144+ DefaultMode : v1 .SecretVolumeSourceDefaultMode ,
145+ }
146+ // Set the defaultMode if set in the kube yaml
147+ validMode , err := isValidDefaultMode (secretSource .DefaultMode )
148+ if err != nil {
149+ return nil , fmt .Errorf ("invalid DefaultMode for secret %q: %w" , secretSource .SecretName , err )
150+ }
151+ if validMode {
152+ kv .DefaultMode = * secretSource .DefaultMode
141153}
142154
143155// returns a byte array of a kube secret data, meaning this needs to go into a string map
@@ -191,8 +203,9 @@ func VolumeFromPersistentVolumeClaim(claim *v1.PersistentVolumeClaimVolumeSource
191203func VolumeFromConfigMap (configMapVolumeSource * v1.ConfigMapVolumeSource , configMaps []v1.ConfigMap ) (* KubeVolume , error ) {
192204var configMap * v1.ConfigMap
193205kv := & KubeVolume {
194- Type : KubeVolumeTypeConfigMap ,
195- Items : map [string ][]byte {},
206+ Type : KubeVolumeTypeConfigMap ,
207+ Items : map [string ][]byte {},
208+ DefaultMode : v1 .ConfigMapVolumeSourceDefaultMode ,
196209}
197210for _ , cm := range configMaps {
198211if cm .Name == configMapVolumeSource .Name {
@@ -203,6 +216,14 @@ func VolumeFromConfigMap(configMapVolumeSource *v1.ConfigMapVolumeSource, config
203216break
204217}
205218}
219+ // Set the defaultMode if set in the kube yaml
220+ validMode , err := isValidDefaultMode (configMapVolumeSource .DefaultMode )
221+ if err != nil {
222+ return nil , fmt .Errorf ("invalid DefaultMode for configMap %q: %w" , configMapVolumeSource .Name , err )
223+ }
224+ if validMode {
225+ kv .DefaultMode = * configMapVolumeSource .DefaultMode
226+ }
206227
207228if configMap == nil {
208229// If the volumeSource was optional, move on even if a matching configmap wasn't found
@@ -279,3 +300,14 @@ func InitializeVolumes(specVolumes []v1.Volume, configMaps []v1.ConfigMap, secre
279300
280301return volumes , nil
281302}
303+
304+ // isValidDefaultMode returns true if mode is between 0 and 0777
305+ func isValidDefaultMode (mode * int32 ) (bool , error ) {
306+ if mode == nil {
307+ return false , nil
308+ }
309+ if * mode >= 0 && * mode <= int32 (os .ModePerm ) {
310+ return true , nil
311+ }
312+ return false , errors .New ("must be between 0000 and 0777" )
313+ }
0 commit comments