|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace NewRelic.Agent.Extensions.AwsSdk |
| 8 | +{ |
| 9 | + public class ArnBuilder |
| 10 | + { |
| 11 | + public readonly string Partition; |
| 12 | + public readonly string Region; |
| 13 | + public readonly string AccountId; |
| 14 | + |
| 15 | + public ArnBuilder(string partition, string region, string accountId) |
| 16 | + { |
| 17 | + Partition = string.IsNullOrEmpty(partition) ? "aws" : partition; |
| 18 | + Region = string.IsNullOrEmpty(region) ? "(unknown)" : region; |
| 19 | + AccountId = accountId ?? ""; |
| 20 | + } |
| 21 | + |
| 22 | + public string Build(string service, string resource) => ConstructArn(Partition, service, Region, AccountId, resource); |
| 23 | + |
| 24 | + // This is the full regex pattern for a Lambda ARN: |
| 25 | + // (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}((-gov)|(-iso([a-z]?)))?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-_\.]+)(:(\$LATEST|[a-zA-Z0-9-_]+))? |
| 26 | + |
| 27 | + // If it's a full ARN, it has to start with 'arn:' |
| 28 | + // A partial ARN can contain up to 5 segments separated by ':' |
| 29 | + // 1. Region |
| 30 | + // 2. Account ID |
| 31 | + // 3. 'function' (fixed string) |
| 32 | + // 4. Function name |
| 33 | + // 5. Alias or version |
| 34 | + // Only the function name is required, the rest are all optional. e.g. you could have region and function name and nothing else |
| 35 | + public string BuildFromPartialLambdaArn(string invocationName) |
| 36 | + { |
| 37 | + if (invocationName.StartsWith("arn:")) |
| 38 | + { |
| 39 | + return invocationName; |
| 40 | + } |
| 41 | + var segments = invocationName.Split(':'); |
| 42 | + string functionName = null; |
| 43 | + string alias = null; |
| 44 | + string fallback = null; |
| 45 | + string region = null; |
| 46 | + string accountId = null; |
| 47 | + |
| 48 | + // If there's only one segment, assume it's the function name |
| 49 | + if (segments.Length == 1) |
| 50 | + { |
| 51 | + functionName = segments[0]; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // All we should need is the function name, but if we find a region or account ID, we'll use it |
| 56 | + // since it should be more accurate |
| 57 | + foreach (var segment in segments) |
| 58 | + { |
| 59 | + // A string that looks like a region or account ID could also be the function name |
| 60 | + // Assume it's the former, unless we never find a function name |
| 61 | + if (LooksLikeARegion(segment)) |
| 62 | + { |
| 63 | + if (string.IsNullOrEmpty(region)) |
| 64 | + { |
| 65 | + region = segment; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + fallback = segment; |
| 70 | + } |
| 71 | + continue; |
| 72 | + } |
| 73 | + else if (LooksLikeAnAccountId(segment)) |
| 74 | + { |
| 75 | + if (string.IsNullOrEmpty(accountId)) |
| 76 | + { |
| 77 | + accountId = segment; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + fallback = segment; |
| 82 | + } |
| 83 | + continue; |
| 84 | + } |
| 85 | + else if (segment == "function") |
| 86 | + { |
| 87 | + continue; |
| 88 | + } |
| 89 | + else if (functionName == null) |
| 90 | + { |
| 91 | + functionName = segment; |
| 92 | + } |
| 93 | + else if (alias == null) |
| 94 | + { |
| 95 | + alias = segment; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (string.IsNullOrEmpty(functionName)) |
| 105 | + { |
| 106 | + if (!string.IsNullOrEmpty(fallback)) |
| 107 | + { |
| 108 | + functionName = fallback; |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + return null; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + accountId = !string.IsNullOrEmpty(accountId) ? accountId : AccountId; |
| 117 | + if (string.IsNullOrEmpty(accountId)) |
| 118 | + { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + // The member Region cannot be blank (it has a default) so we don't need to check it here |
| 123 | + region = !string.IsNullOrEmpty(region) ? region : Region; |
| 124 | + |
| 125 | + if (!string.IsNullOrEmpty(alias)) |
| 126 | + { |
| 127 | + functionName += $":{alias}"; |
| 128 | + } |
| 129 | + return ConstructArn(Partition, "lambda", region, accountId, $"function:{functionName}"); |
| 130 | + } |
| 131 | + |
| 132 | + public override string ToString() |
| 133 | + { |
| 134 | + string idPresent = string.IsNullOrEmpty(AccountId) ? "[Missing]" : "[Present]"; |
| 135 | + |
| 136 | + return $"Partition: {Partition}, Region: {Region}, AccountId: {idPresent}"; |
| 137 | + } |
| 138 | + |
| 139 | + private static Regex RegionRegex = new Regex(@"^[a-z]{2}((-gov)|(-iso([a-z]?)))?-[a-z]+-\d{1}$", RegexOptions.Compiled); |
| 140 | + private static bool LooksLikeARegion(string text) => RegionRegex.IsMatch(text); |
| 141 | + private static bool LooksLikeAnAccountId(string text) => (text.Length == 12) && text.All(c => c >= '0' && c <= '9'); |
| 142 | + |
| 143 | + private string ConstructArn(string partition, string service, string region, string accountId, string resource) |
| 144 | + { |
| 145 | + if (string.IsNullOrEmpty(partition) || string.IsNullOrEmpty(region) || string.IsNullOrEmpty(accountId) |
| 146 | + || string.IsNullOrEmpty(service) || string.IsNullOrEmpty(resource)) |
| 147 | + { |
| 148 | + return null; |
| 149 | + } |
| 150 | + return "arn:" + partition + ":" + service + ":" + region + ":" + accountId + ":" + resource; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments