1 /*
2  * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.services.s3.internal;
16
17 import com.amazonaws.services.s3.model.IllegalBucketNameException;
18
19 import java.util.regex.Pattern;
20
21 /**
22  * Utilities for working with Amazon S3 bucket names, such as validation and
23  * checked to see if they are compatible with DNS addressing.
24  */

25 public enum BucketNameUtils {
26     ;
27     private static final int MIN_BUCKET_NAME_LENGTH = 3;
28     private static final int MAX_BUCKET_NAME_LENGTH = 63;
29
30     private static final Pattern ipAddressPattern = Pattern.compile("(\\d+\\.){3}\\d+");
31
32     /**
33      * Validates that the specified bucket name is valid for Amazon S3 V2 naming
34      * (i.e. DNS addressable in virtual host style). Throws an
35      * IllegalArgumentException if the bucket name is not valid.
36      * <p>
37      * S3 bucket naming guidelines are specified in <a href="http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?BucketRestrictions.html"
38      * > http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?
39      * BucketRestrictions.html</a>
40      *
41      * @param bucketName
42      *            The bucket name to validate.
43      *
44      * @throws IllegalBucketNameException
45      *             If the specified bucket name doesn't follow Amazon S3's
46      *             guidelines.
47      */

48     public static void validateBucketName(final String bucketName) {
49         isValidV2BucketName(bucketName, true);
50     }
51
52     /**
53      * Returns true if the specified bucket name can be addressed using V2,
54      * virtual host style, addressing. Otherwise, returns false indicating that
55      * the bucket must be addressed using V1, path style, addressing.
56      *
57      * @param bucketName
58      *            The name of the bucket to check.
59      *
60      * @return True if the specified bucket name can be addressed in V2, virtual
61      *         host style, addressing otherwise false if V1, path style,
62      *         addressing is required.
63      */

64     public static boolean isValidV2BucketName(String bucketName) {
65         return isValidV2BucketName(bucketName, false);
66     }
67
68     /**
69      * Convience method that allows the DNS rules to be altered for different SDKs.
70      */

71     public static boolean isDNSBucketName(String bucketName) {
72         return isValidV2BucketName( bucketName );
73     }
74
75     /**
76      * Validate whether the given input is a valid bucket name. If throwOnError
77      * is truethrow an IllegalArgumentException if validation fails. If
78      * false, simply return 'false'.
79      *
80      * @param bucketName the name of the bucket
81      * @param throwOnError true to throw exceptions on failure
82      * @return true if the name is valid, false if not
83      */

84     private static boolean isValidV2BucketName(final String bucketName,
85                                         final boolean throwOnError) {
86
87         if (bucketName == null) {
88             return exception(throwOnError, "Bucket name cannot be null");
89         }
90
91         if (bucketName.length() < MIN_BUCKET_NAME_LENGTH ||
92             bucketName.length() > MAX_BUCKET_NAME_LENGTH) {
93
94             return exception(
95                 throwOnError,
96                 "Bucket name should be between " + MIN_BUCKET_NAME_LENGTH + " and " + MAX_BUCKET_NAME_LENGTH +" characters long"
97             );
98         }
99
100         if (ipAddressPattern.matcher(bucketName).matches()) {
101             return exception(
102                     throwOnError,
103                     "Bucket name must not be formatted as an IP Address"
104             );
105         }
106
107         char previous = '\0';
108
109         for (int i = 0; i < bucketName.length(); ++i) {
110             char next = bucketName.charAt(i);
111
112             if (next >= 'A' && next <= 'Z') {
113                 return exception(
114                     throwOnError,
115                     "Bucket name should not contain uppercase characters"
116                 );
117             }
118
119             if (next == ' ' || next == '\t' || next == '\r' || next == '\n') {
120                 return exception(
121                     throwOnError,
122                     "Bucket name should not contain white space"
123                 );
124             }
125
126             if (next == '.') {
127                 if (previous == '\0') {
128                     return exception(
129                         throwOnError,
130                         "Bucket name should not begin with a period"
131                     );
132                 }
133                 if (previous == '.') {
134                     return exception(
135                         throwOnError,
136                         "Bucket name should not contain two adjacent periods"
137                     );
138                 }
139                 if (previous == '-') {
140                     return exception(
141                         throwOnError,
142                         "Bucket name should not contain dashes next to periods"
143                     );
144                 }
145             } else if (next == '-') {
146                 if (previous == '.') {
147                     return exception(
148                         throwOnError,
149                         "Bucket name should not contain dashes next to periods"
150                     );
151                 }
152                 if (previous == '\0') {
153                     return exception(
154                             throwOnError,
155                             "Bucket name should not begin with a '-'"
156                     );
157                 }
158             } else if ((next < '0')
159                        || (next > '9' && next < 'a')
160                        || (next > 'z')) {
161
162                 return exception(
163                      throwOnError,
164                     "Bucket name should not contain '" + next + "'"
165                 );
166             }
167
168             previous = next;
169         }
170
171         if (previous == '.' || previous == '-') {
172             return exception(
173                 throwOnError,
174                 "Bucket name should not end with '-' or '.'"
175             );
176         }
177
178         return true;
179     }
180
181     /**
182      * If 'exception' is truethrow an IllegalBucketNameException with the given
183      * message. Otherwise, silently return false.
184      *
185      * @param exception true to throw an exception
186      * @param message the message for the exception
187      * @return false if 'exception' is false
188      */

189     private static boolean exception(final boolean exception, final String message) {
190         if (exception) {
191             throw new IllegalBucketNameException(message);
192         }
193         return false;
194     }
195 }
196