Skip to content

Commit 2a1d6e0

Browse files
committed
Addressed review comments:
1. pkg/constants/constants.go - Added new line after header comment - Moved inline comment for var NoGID to an above the code comment. 2. pkg/rotation/reconciler.go pkg/secrets-store/nodeserver.go - Now use common function: fileutil.ParseFSGroup instead of ParseInt directly - Un-necessary logs removed. 3. pkg/secrets-store/utils.go - Removed a not so useful log 4. pkg/util/fileutil/filesystem.go - Defines common function ParseFSGroup used by reconcile and nodeserver 5. pkg/util/fileutil/filesystem_test.go - Tests ParseFSGroup
1 parent a674b81 commit 2a1d6e0

File tree

6 files changed

+99
-27
lines changed

6 files changed

+99
-27
lines changed

pkg/constants/constants.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
1617
package constants
1718

1819
const (
19-
NoGID = int64(-1) // Use the default gid -1 to indicate no change in FSGroup
20+
// NoGID is the default gid -1 to indicate no change in FSGroup
21+
NoGID = int64(-1)
2022
)

pkg/rotation/reconciler.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"os"
24-
"strconv"
2524
"strings"
2625
"time"
2726

2827
secretsstorev1 "sigs.k8s.io/secrets-store-csi-driver/apis/v1"
2928
"sigs.k8s.io/secrets-store-csi-driver/controllers"
3029
secretsStoreClient "sigs.k8s.io/secrets-store-csi-driver/pkg/client/clientset/versioned"
31-
"sigs.k8s.io/secrets-store-csi-driver/pkg/constants"
3230
internalerrors "sigs.k8s.io/secrets-store-csi-driver/pkg/errors"
3331
"sigs.k8s.io/secrets-store-csi-driver/pkg/k8s"
3432
secretsstore "sigs.k8s.io/secrets-store-csi-driver/pkg/secrets-store"
@@ -400,15 +398,12 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *secretsstorev1.Secret
400398
r.generateEvent(pod, corev1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("failed to lookup provider client: %q", providerName))
401399
return fmt.Errorf("failed to lookup provider client: %q", providerName)
402400
}
403-
gid := constants.NoGID
404-
if len(spcps.Status.FSGroup) > 0 {
405-
gid, err = strconv.ParseInt(spcps.Status.FSGroup, 10, 64)
406-
if err != nil {
407-
errorReason = internalerrors.FailedToParseFSGroup
408-
errStr := fmt.Sprintf("failed to rotate objects for pod %s/%s, invalid FSGroup:%s", spcps.Namespace, spcps.Status.PodName, spcps.Status.FSGroup)
409-
r.generateEvent(pod, corev1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("%s, err: %v", errStr, err))
410-
return fmt.Errorf("%s, err: %w", errStr, err)
411-
}
401+
gid, err := fileutil.ParseFSGroup(spcps.Status.FSGroup)
402+
if err != nil {
403+
errorReason = internalerrors.FailedToParseFSGroup
404+
errStr := fmt.Sprintf("failed to rotate objects for pod %s/%s, invalid FSGroup:%s", spcps.Namespace, spcps.Status.PodName, spcps.Status.FSGroup)
405+
r.generateEvent(pod, corev1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("%s, err: %v", errStr, err))
406+
return fmt.Errorf("%s, err: %w", errStr, err)
412407
}
413408
klog.V(5).InfoS("updating the secret content", "pod", klog.ObjectRef{Namespace: spcps.Namespace, Name: spcps.Status.PodName}, "FSGroup", gid)
414409
newObjectVersions, errorReason, err := secretsstore.MountContent(ctx, providerClient, string(paramsJSON), string(secretsJSON), spcps.Status.TargetPath, string(permissionJSON), oldObjectVersions, gid)

pkg/secrets-store/nodeserver.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ import (
2323
"fmt"
2424
"os"
2525
"path/filepath"
26-
"strconv"
2726
"time"
2827

29-
"sigs.k8s.io/secrets-store-csi-driver/pkg/constants"
3028
internalerrors "sigs.k8s.io/secrets-store-csi-driver/pkg/errors"
3129
"sigs.k8s.io/secrets-store-csi-driver/pkg/k8s"
3230
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/fileutil"
@@ -144,19 +142,12 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
144142

145143
klog.V(2).InfoS("node publish volume", "target", targetPath, "volumeId", volumeID, "mount flags", mountFlags, "volumeCapabilities", req.VolumeCapability.String())
146144

147-
gid := constants.NoGID // Group ID to Chown the volume contents to
145+
// Group ID to Chown the volume contents to
148146
mountVol := req.VolumeCapability.GetMount()
149-
if len(mountVol.GetVolumeMountGroup()) > 0 {
150-
fsGroupStr := mountVol.GetVolumeMountGroup()
151-
klog.V(5).Info("fsGroupStr: %v\n", fsGroupStr)
152-
gid, err = strconv.ParseInt(fsGroupStr, 10, 64)
153-
klog.V(5).Info("converted gid: %v\n", gid)
154-
if err != nil {
155-
klog.ErrorS(err, "failed to mount secrets store object content", "pod", klog.ObjectRef{Namespace: podNamespace, Name: podName}, "FSGroup: ", fsGroupStr)
156-
return nil, status.Error(codes.InvalidArgument, "Error parsing FSGroup")
157-
}
158-
} else {
159-
klog.V(5).InfoS("mount group not set", "targetPath", targetPath, "pod", klog.ObjectRef{Namespace: podNamespace, Name: podName})
147+
gid, err := fileutil.ParseFSGroup(mountVol.GetVolumeMountGroup())
148+
if err != nil {
149+
klog.ErrorS(err, "failed to mount secrets store object content due to invalid FSGroup", "pod", klog.ObjectRef{Namespace: podNamespace, Name: podName}, "fsGroup", mountVol.GetVolumeMountGroup())
150+
return nil, status.Errorf(codes.InvalidArgument, "error parsing FSGroup: %v", err)
160151
}
161152

162153
if isMockProvider(providerName) {

pkg/secrets-store/utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func createOrUpdateSecretProviderClassPodStatus(ctx context.Context, c client.Cl
105105
if gid != constants.NoGID {
106106
fsGroup = strconv.FormatInt(gid, 10)
107107
}
108-
klog.V(5).InfoS("setting gid in spcps", "pod", klog.ObjectRef{Namespace: namespace, Name: podname}, "gid", gid, "gidStr", fsGroup)
109108
spcPodStatus := &secretsstorev1.SecretProviderClassPodStatus{
110109
ObjectMeta: metav1.ObjectMeta{
111110
Name: spcpsName,

pkg/util/fileutil/filesystem.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121
"os"
2222
"path/filepath"
2323
"regexp"
24+
"strconv"
2425
"strings"
26+
27+
"sigs.k8s.io/secrets-store-csi-driver/pkg/constants"
2528
)
2629

2730
var (
@@ -127,3 +130,15 @@ func GetVolumeNameFromTargetPath(targetPath string) string {
127130
}
128131
return match[2]
129132
}
133+
134+
// ParseFSGroup parses the FSGroup string and returns the GID as int64.
135+
// If fsGroupStr is empty, returns constants.NoGID.
136+
// Returns an error if the fsGroupStr cannot be parsed as a valid non-negative int64.
137+
func ParseFSGroup(fsGroupStr string) (int64, error) {
138+
if len(fsGroupStr) == 0 {
139+
return constants.NoGID, nil
140+
}
141+
// Non-sentinel negative GID is invalid and thus we use ParseUint here.
142+
gid, err := strconv.ParseUint(fsGroupStr, 10, 63)
143+
return int64(gid), err
144+
}

pkg/util/fileutil/filesystem_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/google/go-cmp/cmp"
2626
"github.com/google/go-cmp/cmp/cmpopts"
27+
"sigs.k8s.io/secrets-store-csi-driver/pkg/constants"
2728
)
2829

2930
func TestGetMountedFiles(t *testing.T) {
@@ -333,3 +334,72 @@ func TestGetVolumeNameFromTargetPath(t *testing.T) {
333334
}
334335
}
335336
}
337+
338+
func TestParseFSGroup(t *testing.T) {
339+
cases := []struct {
340+
name string
341+
fsGroupStr string
342+
want int64
343+
expectedErr bool
344+
}{
345+
{
346+
name: "empty string returns NoGID",
347+
fsGroupStr: "",
348+
want: constants.NoGID,
349+
expectedErr: false,
350+
},
351+
{
352+
name: "valid gid",
353+
fsGroupStr: "1000",
354+
want: 1000,
355+
expectedErr: false,
356+
},
357+
{
358+
name: "valid gid zero",
359+
fsGroupStr: "0",
360+
want: 0,
361+
expectedErr: false,
362+
},
363+
{
364+
name: "valid large gid",
365+
fsGroupStr: "65534",
366+
want: 65534,
367+
expectedErr: false,
368+
},
369+
{
370+
name: "invalid gid - non-numeric",
371+
fsGroupStr: "INVALID",
372+
expectedErr: true,
373+
},
374+
{
375+
name: "invalid gid - float",
376+
fsGroupStr: "123.45",
377+
expectedErr: true,
378+
},
379+
{
380+
name: "invalid gid - with spaces",
381+
fsGroupStr: "123 456",
382+
expectedErr: true,
383+
},
384+
{
385+
name: "negative gid",
386+
fsGroupStr: "-23",
387+
expectedErr: true,
388+
},
389+
}
390+
391+
for _, tc := range cases {
392+
t.Run(tc.name, func(t *testing.T) {
393+
got, err := ParseFSGroup(tc.fsGroupStr)
394+
if tc.expectedErr {
395+
if err == nil {
396+
t.Errorf("ParseFSGroup(%q) expected error but got none", tc.fsGroupStr)
397+
}
398+
} else if err != nil {
399+
t.Errorf("ParseFSGroup(%q) unexpected error: %v", tc.fsGroupStr, err)
400+
} else if got != tc.want {
401+
t.Errorf("ParseFSGroup(%q) = %v, want %v", tc.fsGroupStr, got, tc.want)
402+
}
403+
})
404+
}
405+
}

0 commit comments

Comments
 (0)