1818
1919import static com .cloud .utils .NumbersUtil .toHumanReadableSize ;
2020
21+ import java .io .UnsupportedEncodingException ;
2122import java .math .BigDecimal ;
2223import java .math .BigInteger ;
2324import java .net .URI ;
2425import java .net .URISyntaxException ;
26+ import java .net .URLDecoder ;
2527import java .net .UnknownHostException ;
2628import java .nio .file .Files ;
2729import java .sql .PreparedStatement ;
132134import org .apache .cloudstack .storage .object .ObjectStoreEntity ;
133135import org .apache .cloudstack .storage .to .VolumeObjectTO ;
134136import org .apache .commons .collections .CollectionUtils ;
135- import org .apache .commons .lang3 . EnumUtils ;
137+ import org .apache .commons .collections . MapUtils ;
136138import org .apache .commons .lang .time .DateUtils ;
139+ import org .apache .commons .lang3 .EnumUtils ;
137140import org .apache .commons .lang3 .StringUtils ;
138141import org .apache .log4j .Logger ;
139142import org .springframework .stereotype .Component ;
254257import com .cloud .vm .VirtualMachine .State ;
255258import com .cloud .vm .dao .VMInstanceDao ;
256259import com .google .common .collect .Sets ;
257- import java .io .UnsupportedEncodingException ;
258- import java .net .URLDecoder ;
259260
260261
261262@ Component
@@ -684,12 +685,21 @@ public boolean stop() {
684685 return true ;
685686 }
686687
687- private DataStore createLocalStorage (Map <String , Object > poolInfos ) throws ConnectionException {
688+ protected String getValidatedPareForLocalStorage (Object obj , String paramName ) {
689+ String result = obj == null ? null : obj .toString ();
690+ if (StringUtils .isEmpty (result )) {
691+ throw new InvalidParameterValueException (String .format ("Invalid %s provided" , paramName ));
692+ }
693+ return result ;
694+ }
695+
696+ protected DataStore createLocalStorage (Map <String , Object > poolInfos ) throws ConnectionException {
688697 Object existingUuid = poolInfos .get ("uuid" );
689698 if ( existingUuid == null ){
690699 poolInfos .put ("uuid" , UUID .randomUUID ().toString ());
691700 }
692- String hostAddress = poolInfos .get ("host" ).toString ();
701+ String hostAddress = getValidatedPareForLocalStorage (poolInfos .get ("host" ), "host" );
702+ String hostPath = getValidatedPareForLocalStorage (poolInfos .get ("hostPath" ), "path" );
693703 Host host = _hostDao .findByName (hostAddress );
694704
695705 if ( host == null ) {
@@ -708,8 +718,8 @@ private DataStore createLocalStorage(Map<String, Object> poolInfos) throws Conne
708718
709719 StoragePoolInfo pInfo = new StoragePoolInfo (poolInfos .get ("uuid" ).toString (),
710720 host .getPrivateIpAddress (),
711- poolInfos . get ( " hostPath" ). toString () ,
712- poolInfos . get ( " hostPath" ). toString () ,
721+ hostPath ,
722+ hostPath ,
713723 StoragePoolType .Filesystem ,
714724 capacityBytes ,
715725 0 ,
@@ -809,6 +819,7 @@ protected String createLocalStoragePoolName(Host host, StoragePoolInfo storagePo
809819 public PrimaryDataStoreInfo createPool (CreateStoragePoolCmd cmd ) throws ResourceInUseException , IllegalArgumentException , UnknownHostException , ResourceUnavailableException {
810820 String providerName = cmd .getStorageProviderName ();
811821 Map <String ,String > uriParams = extractUriParamsAsMap (cmd .getUrl ());
822+ boolean isFileScheme = "file" .equals (uriParams .get ("scheme" ));
812823 DataStoreProvider storeProvider = _dataStoreProviderMgr .getDataStoreProvider (providerName );
813824
814825 if (storeProvider == null ) {
@@ -822,7 +833,10 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource
822833 Long podId = cmd .getPodId ();
823834 Long zoneId = cmd .getZoneId ();
824835
825- ScopeType scopeType = uriParams .get ("scheme" ).toString ().equals ("file" ) ? ScopeType .HOST : ScopeType .CLUSTER ;
836+ ScopeType scopeType = ScopeType .CLUSTER ;
837+ if (isFileScheme ) {
838+ scopeType = ScopeType .HOST ;
839+ }
826840 String scope = cmd .getScope ();
827841 if (scope != null ) {
828842 try {
@@ -889,12 +903,14 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource
889903 params .put ("managed" , cmd .isManaged ());
890904 params .put ("capacityBytes" , cmd .getCapacityBytes ());
891905 params .put ("capacityIops" , cmd .getCapacityIops ());
892- params .putAll (uriParams );
906+ if (MapUtils .isNotEmpty (uriParams )) {
907+ params .putAll (uriParams );
908+ }
893909
894910 DataStoreLifeCycle lifeCycle = storeProvider .getDataStoreLifeCycle ();
895911 DataStore store = null ;
896912 try {
897- if (params . get ( "scheme" ). toString (). equals ( "file" ) ) {
913+ if (isFileScheme ) {
898914 store = createLocalStorage (params );
899915 } else {
900916 store = lifeCycle .initialize (params );
@@ -923,42 +939,55 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource
923939 return (PrimaryDataStoreInfo )_dataStoreMgr .getDataStore (store .getId (), DataStoreRole .Primary );
924940 }
925941
926- private Map <String ,String > extractUriParamsAsMap (String url ){
942+ protected Map <String ,String > extractUriParamsAsMap (String url ) {
927943 Map <String ,String > uriParams = new HashMap <>();
928- UriUtils .UriInfo uriInfo = UriUtils .getUriInfo (url );
944+ UriUtils .UriInfo uriInfo ;
945+ try {
946+ uriInfo = UriUtils .getUriInfo (url );
947+ } catch (CloudRuntimeException cre ) {
948+ if (s_logger .isDebugEnabled ()) {
949+ s_logger .debug (String .format ("URI validation for url: %s failed, returning empty uri params" , url ));
950+ }
951+ return uriParams ;
952+ }
929953
930954 String scheme = uriInfo .getScheme ();
931955 String storageHost = uriInfo .getStorageHost ();
932956 String storagePath = uriInfo .getStoragePath ();
933- try {
934- if (scheme == null ) {
935- throw new InvalidParameterValueException ("scheme is null " + url + ", add nfs:// (or cifs://) as a prefix" );
936- } else if (scheme .equalsIgnoreCase ("nfs" )) {
937- if (storageHost == null || storagePath == null || storageHost .trim ().isEmpty () || storagePath .trim ().isEmpty ()) {
938- throw new InvalidParameterValueException ("host or path is null, should be nfs://hostname/path" );
939- }
940- } else if (scheme .equalsIgnoreCase ("cifs" )) {
941- // Don't validate against a URI encoded URI.
957+ if (scheme == null ) {
958+ if (s_logger .isDebugEnabled ()) {
959+ s_logger .debug (String .format ("Scheme for url: %s is not found, returning empty uri params" , url ));
960+ }
961+ return uriParams ;
962+ }
963+ boolean isHostOrPathBlank = StringUtils .isAnyBlank (storagePath , storageHost );
964+ if (scheme .equalsIgnoreCase ("nfs" )) {
965+ if (isHostOrPathBlank ) {
966+ throw new InvalidParameterValueException ("host or path is null, should be nfs://hostname/path" );
967+ }
968+ } else if (scheme .equalsIgnoreCase ("cifs" )) {
969+ // Don't validate against a URI encoded URI.
970+ try {
942971 URI cifsUri = new URI (url );
943972 String warnMsg = UriUtils .getCifsUriParametersProblems (cifsUri );
944973 if (warnMsg != null ) {
945974 throw new InvalidParameterValueException (warnMsg );
946975 }
947- } else if (scheme .equalsIgnoreCase ("sharedMountPoint" )) {
948- if (storagePath == null ) {
949- throw new InvalidParameterValueException ("host or path is null, should be sharedmountpoint://localhost/path" );
950- }
951- } else if (scheme .equalsIgnoreCase ("rbd" )) {
952- if (storagePath == null ) {
953- throw new InvalidParameterValueException ("host or path is null, should be rbd://hostname/pool" );
954- }
955- } else if (scheme .equalsIgnoreCase ("gluster" )) {
956- if (storageHost == null || storagePath == null || storageHost .trim ().isEmpty () || storagePath .trim ().isEmpty ()) {
957- throw new InvalidParameterValueException ("host or path is null, should be gluster://hostname/volume" );
958- }
976+ } catch (URISyntaxException e ) {
977+ throw new InvalidParameterValueException (url + " is not a valid uri" );
978+ }
979+ } else if (scheme .equalsIgnoreCase ("sharedMountPoint" )) {
980+ if (storagePath == null ) {
981+ throw new InvalidParameterValueException ("host or path is null, should be sharedmountpoint://localhost/path" );
982+ }
983+ } else if (scheme .equalsIgnoreCase ("rbd" )) {
984+ if (storagePath == null ) {
985+ throw new InvalidParameterValueException ("host or path is null, should be rbd://hostname/pool" );
986+ }
987+ } else if (scheme .equalsIgnoreCase ("gluster" )) {
988+ if (isHostOrPathBlank ) {
989+ throw new InvalidParameterValueException ("host or path is null, should be gluster://hostname/volume" );
959990 }
960- } catch (URISyntaxException e ) {
961- throw new InvalidParameterValueException (url + " is not a valid uri" );
962991 }
963992
964993 String hostPath = null ;
@@ -975,7 +1004,9 @@ private Map<String,String> extractUriParamsAsMap(String url){
9751004 uriParams .put ("host" , storageHost );
9761005 uriParams .put ("hostPath" , hostPath );
9771006 uriParams .put ("userInfo" , uriInfo .getUserInfo ());
978- uriParams .put ("port" , uriInfo .getPort () + "" );
1007+ if (uriInfo .getPort () > 0 ) {
1008+ uriParams .put ("port" , uriInfo .getPort () + "" );
1009+ }
9791010 return uriParams ;
9801011 }
9811012
0 commit comments