/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using Amazon.Runtime;
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Amazon.S3Control.Internal
{
///
/// Extensions methods added to Amazon.Arn type to help parse S3 specific resources from the ARN.
///
public static class ArnExtensions
{
internal const string ResourceTypeAccessPoint = "accesspoint";
internal const string ResourceTypeBucket = "bucket";
internal const string ResourceTypeOutpost = "outpost";
///
/// Check if aws ARN resembles an outpost ARN
///
/// An AWS ARN to parse
/// True if the ARN contains an outpost resource identifier.
public static bool IsOutpostArn(this Arn arn)
{
return !string.IsNullOrEmpty(arn.Resource) &&
(S3ArnUtils.ArnSplit.Any(i => arn.Resource.StartsWith($"{ResourceTypeOutpost}{i}"))
|| arn.Resource.Equals(ResourceTypeOutpost));
}
///
/// Parse an ARN to extract information on S3 outpost access point or bucket ARN
/// and if it is not found or properly formatted, throw an exception
///
/// The ARN to be parsed into an S3 Outposts resource
/// Used to validate
/// A
public static IS3Resource ParseOutpost(this Arn arn, AmazonS3ControlConfig config = null)
{
if (string.IsNullOrEmpty(arn.Resource))
{
throw new AmazonClientException("ARN resource can not be null");
}
if (!IsOutpostArn(arn))
{
throw new AmazonClientException("ARN resource does not resemble an outpost access point");
}
var parts = arn.Resource.Split(S3ArnUtils.ArnSplit, 5);
if (parts.Length < 4 || (!string.Equals(parts[2], ResourceTypeAccessPoint) && !string.Equals(parts[2], ResourceTypeBucket)))
{
throw new AmazonClientException("Invalid ARN, outpost resource format is incorrect");
}
if (arn.Region.EndsWith("-fips", StringComparison.OrdinalIgnoreCase))
{
throw new AmazonClientException("Invalid ARN, FIPS region not allowed in ARN");
}
return new S3OutpostResource(arn)
{
OutpostId = parts[1],
Type = (S3ResourceType)Enum.Parse(typeof(S3ResourceType), parts[2], true),
Name = parts[3],
Key = parts.Length > 4 ? parts[4] : null
};
}
///
/// Check if the ARN has a valid service name
///
/// The ARN which is being validated
public static bool IsValidService(this Arn arn)
{
return arn.Service.Equals(S3ArnUtils.S3OutpostsService);
}
///
/// Check if the ARN has a valid Account ID
///
/// The ARN which is being validated
public static bool HasValidAccountId(this Arn arn)
{
return string.IsNullOrEmpty(arn.AccountId) || (arn.AccountId.Length == 12 && arn.AccountId.ToCharArray().All(x => char.IsDigit(x)));
}
}
}