| | 1 | | // Copyright (c) Microsoft and contributors. All rights reserved. |
| | 2 | | // |
| | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | 4 | | // you may not use this file except in compliance with the License. |
| | 5 | | // You may obtain a copy of the License at |
| | 6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | 7 | | // |
| | 8 | | // Unless required by applicable law or agreed to in writing, software |
| | 9 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | 10 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | 11 | | // |
| | 12 | | // See the License for the specific language governing permissions and |
| | 13 | | // limitations under the License. |
| | 14 | |
|
| | 15 | | using System; |
| | 16 | | using System.Collections.Generic; |
| | 17 | | using System.Linq; |
| | 18 | | using System.Security.Cryptography; |
| | 19 | | using System.Text; |
| | 20 | | using System.Text.RegularExpressions; |
| | 21 | | using System.Threading.Tasks; |
| | 22 | |
|
| | 23 | | namespace Microsoft.Azure.Batch.Conventions.Files.Utilities |
| | 24 | | { |
| | 25 | | internal static class ContainerNameUtils |
| | 26 | | { |
| 1 | 27 | | private static readonly Regex PermittedContainerNamePattern = new Regex("^[a-z0-9][-a-z0-9]*$"); |
| | 28 | |
|
| | 29 | | // new instance each time because a shared instance would not be thread-safe |
| | 30 | | #if FullNetFx |
| | 31 | | private static readonly Func<HashAlgorithm> hasher = () => new SHA1CryptoServiceProvider(); |
| | 32 | | #else |
| 514 | 33 | | private static readonly Func<HashAlgorithm> hasher = () => SHA1.Create(); |
| | 34 | | #endif |
| 1 | 35 | | private static readonly int MaxJobIdLengthInMungedContainerName = 15; // must be <= 63 - "job-".Length - 1 (hyp |
| 1 | 36 | | private static readonly char[] ForbiddenLeadingTrailingContainerNameChars = new[] { '-' }; |
| 1 | 37 | | private static readonly Regex InvalidContainerCharacters = new Regex("[:_-]+"); |
| | 38 | | private const string ContainerPrefix = "job-"; |
| 1 | 39 | | private static readonly int MaxUsableJobIdLength = 63 - ContainerPrefix.Length; |
| | 40 | |
|
| | 41 | | internal static string GetSafeContainerName(string jobId) |
| | 42 | | { |
| 912 | 43 | | return ContainerPrefix + GetUnprefixedSafeContainerName(jobId); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | private static string GetUnprefixedSafeContainerName(string jobId) |
| | 47 | | { |
| 912 | 48 | | jobId = jobId.ToLowerInvariant(); // it's safe to do this early as job ids cannot differ only by case, so t |
| | 49 | |
|
| | 50 | | // Check that job-{jobId} won't overflow the maximum container name length. |
| | 51 | | // We don't need a minimum length check because the "job-" prefix ensures |
| | 52 | | // that the actual container name will be >= 3 characters. |
| 912 | 53 | | if (jobId.Length > MaxUsableJobIdLength) |
| | 54 | | { |
| 70 | 55 | | return TransformToContainerName(jobId); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | // Check that the jobId contains only characters that are valid in a container |
| | 59 | | // name, and doesn't start with a '-'. |
| 842 | 60 | | if (!PermittedContainerNamePattern.IsMatch(jobId)) |
| | 61 | | { |
| 440 | 62 | | return TransformToContainerName(jobId); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // Check that jobId doesn't end with a '-', and doesn't contain consecutive hyphens. |
| 402 | 66 | | if (jobId[jobId.Length - 1] == '-' || jobId.Contains("--")) |
| | 67 | | { |
| 3 | 68 | | return TransformToContainerName(jobId); |
| | 69 | | } |
| | 70 | |
|
| 399 | 71 | | return jobId; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | private static string TransformToContainerName(string str) |
| | 75 | | { |
| 513 | 76 | | var hash = hasher().ComputeHash(Encoding.UTF8.GetBytes(str)); |
| 513 | 77 | | var hashText = ToHex(hash); |
| | 78 | |
|
| 513 | 79 | | var safeStr = MakeSafe(str); |
| | 80 | |
|
| 513 | 81 | | safeStr = safeStr.Trim(ForbiddenLeadingTrailingContainerNameChars); |
| | 82 | |
|
| 513 | 83 | | if (safeStr.Length > MaxJobIdLengthInMungedContainerName) |
| | 84 | | { |
| 454 | 85 | | safeStr = safeStr.Substring(0, MaxJobIdLengthInMungedContainerName) |
| 454 | 86 | | .Trim(ForbiddenLeadingTrailingContainerNameChars); // do this again as truncation may |
| | 87 | | } |
| 59 | 88 | | else if (safeStr.Length == 0) |
| | 89 | | { |
| 4 | 90 | | safeStr = "job"; |
| | 91 | | } |
| | 92 | |
|
| 513 | 93 | | return safeStr + "-" + hashText; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | private static string MakeSafe(string rawString) |
| | 97 | | { |
| 513 | 98 | | return InvalidContainerCharacters.Replace(rawString, "-"); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | private static string ToHex(byte[] bytes) |
| | 102 | | { |
| 513 | 103 | | var sb = new StringBuilder(bytes.Length * 2); |
| 21546 | 104 | | foreach (var b in bytes) |
| | 105 | | { |
| 10260 | 106 | | sb.AppendFormat(b.ToString("x2")); |
| | 107 | | } |
| 513 | 108 | | return sb.ToString(); |
| | 109 | | } |
| | 110 | | } |
| | 111 | | } |