/*
* Copyright 2010-2013 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 System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Amazon.Auth.AccessControlPolicy
{
///
/// A factory for creating resources to be used in the policy.
///
public static class ResourceFactory
{
///
/// Constructs a new bucket resource that represents the the specified bucket
/// but not any of the contained objects.
///
/// The name of the bucket represented by this AWS access control
/// policy resource.
public static Resource NewS3BucketResource(string bucketName)
{
if (bucketName == null)
{
throw new ArgumentNullException("bucketName");
}
return new Resource("arn:aws:s3:::" + bucketName);
}
///
/// Constructs a new object resource that represents the specified objects.
/// The keyPattern argument may contain the '*' wildcard to match multiple
/// objects. For example, an object resource created for bucket 'mybucket'
/// and key pattern 'foo*' will match any object stored in 'mybucket' with a
/// key that starts with 'foo'.
///
/// The name of the bucket containing the object or objects
/// represented by this resource.
/// The key or key pattern, which can optionally contain the '*'
/// wildcard to include multiple objects in the resource.
public static Resource NewS3ObjectResource(string bucketName, string keyPattern)
{
if (bucketName == null)
{
throw new ArgumentNullException("bucketName");
}
if (keyPattern == null)
{
throw new ArgumentNullException("keyPattern");
}
return new Resource(string.Format(CultureInfo.InvariantCulture, "arn:aws:s3:::{0}/{1}", bucketName, keyPattern));
}
///
/// Constructs a new SQS queue resource for an access control policy. A
/// policy statement using this resource will allow or deny actions on the
/// specified queue.
///
/// The AWS account ID of the queue owner.
/// The name of the Amazon SQS queue.
public static Resource NewSQSQueueResource(string accountId, string queueName)
{
return new Resource("/" + FormatAccountId(accountId) + "/" + queueName);
}
private static string FormatAccountId(string accountId)
{
if (accountId == null)
{
throw new ArgumentNullException("accountId");
}
return accountId.Trim().Replace("-", "");
}
}
}