From 1709545db60c5148ebc1ca28bd7b07eff7d54ee0 Mon Sep 17 00:00:00 2001
From: Cyrill Troxler <cyrilltroxler@gmail.com>
Date: Wed, 6 Feb 2019 20:02:28 +0100
Subject: [PATCH] Sanitize the volumeID before creation

* Bucket names cannot contain upper case letters
* Bucket names cannot be longer than 63 chars
---
 pkg/s3/controllerserver.go | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/pkg/s3/controllerserver.go b/pkg/s3/controllerserver.go
index b8cf456..21b2900 100644
--- a/pkg/s3/controllerserver.go
+++ b/pkg/s3/controllerserver.go
@@ -17,7 +17,11 @@ limitations under the License.
 package s3
 
 import (
+	"crypto/sha1"
+	"encoding/hex"
 	"fmt"
+	"io"
+	"strings"
 
 	"github.com/golang/glog"
 	"golang.org/x/net/context"
@@ -34,7 +38,7 @@ type controllerServer struct {
 }
 
 func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
-	volumeID := req.GetName()
+	volumeID := sanitizeVolumeID(req.GetName())
 
 	if err := cs.Driver.ValidateControllerServiceRequest(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
 		glog.V(3).Infof("invalid create volume req: %v", req)
@@ -156,3 +160,13 @@ func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req
 	}
 	return &csi.ValidateVolumeCapabilitiesResponse{Supported: true, Message: ""}, nil
 }
+
+func sanitizeVolumeID(volumeID string) string {
+	volumeID = strings.ToLower(volumeID)
+	if len(volumeID) > 63 {
+		h := sha1.New()
+		io.WriteString(h, volumeID)
+		volumeID = hex.EncodeToString(h.Sum(nil))
+	}
+	return volumeID
+}