Beispiel #1
0
 public SharedCredentialsFileTester(string fileContents)
 {
     FilePath = Path.GetTempFileName();
     File.WriteAllText(FilePath, fileContents);
     CredentialsFile = new SharedCredentialsFile(FilePath);
 }
Beispiel #2
0
        private static async Task Main(string[] args)
        {
            //var observer = new ExampleDiagnosticObserver();

            //DiagnosticListener.AllListeners.Subscribe(observer);

            var f = new SharedCredentialsFile(SharedCredentialsFile.DefaultFilePath);

            f.TryGetProfile("default", out var profile);

            var credentials = profile.GetAWSCredentials(null);

            var client = new AmazonSQSClient(credentials, RegionEndpoint.EUWest2);

            var options = new SqsPollingQueueReaderOptions {
                QueueUrl = "https://sqs.eu-west-1.amazonaws.com/123456789012/test-queue"
            };

            //using var pollingReader = new SqsPollingQueueReader(options, client, new SqsReceiveDelayCalculator(options), null);

            //using var deleter = new SqsBatchDeleter(new SqsBatchDeletionOptions { MaxWaitForFullBatch = TimeSpan.FromSeconds(10), DrainOnStop = true, QueueUrl = "https://sqs.eu-west-1.amazonaws.com/123456789012/test-queue" }, client);

            //using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));

            //var readingTask = ReadFromChannelAsync(pollingReader.ChannelReader, deleter, cts.Token);

            //deleter.Start(cts.Token);
            //pollingReader.Start(cts.Token);

            //await readingTask;

            //for (var i = 0; i < 26; i++)
            //{
            //    await deleter.AddMessageAsync(new Message{ MessageId = Guid.NewGuid().ToString() }, cts.Token);
            //}

            //deleter.Start(cts.Token);

            //await Task.Delay(TimeSpan.FromSeconds(8), cts.Token);

            //await deleter.AddMessageAsync(new Message { MessageId = Guid.NewGuid().ToString() }, cts.Token);

            //await Task.Delay(TimeSpan.FromSeconds(15), cts.Token);

            //for (var i = 0; i < 11; i++)
            //{
            //    await deleter.AddMessageAsync(new Message { MessageId = Guid.NewGuid().ToString() }, cts.Token);
            //}

            //var messages = Enumerable.Range(0, 57).Select(x => new Message {MessageId = Guid.NewGuid().ToString()}).ToArray();

            //await deleter.AddMessagesAsync(messages, cts.Token);

            //await Task.Delay(TimeSpan.FromSeconds(10), cts.Token);

            //for (var i = 0; i < 2; i++)
            //{
            //    await deleter.AddMessageAsync(new Message{ MessageId = "ABC" }, cts.Token);
            //}

            //await Task.Delay(TimeSpan.FromSeconds(10), cts.Token);

            //await deleter.StopAsync();

            //await Task.Delay(Timeout.Infinite, cts.Token);
        }
Beispiel #3
0
        public void demo()
        {
            try
            {
                var sharedFile = new SharedCredentialsFile();
                if (sharedFile.TryGetProfile("basic_profile", out basicProfile) &&
                    AWSCredentialsFactory.TryGetAWSCredentials(basicProfile, sharedFile, out awsCredentials))
                {
                    using (client = new AmazonS3Client(awsCredentials, basicProfile.Region))
                    {
                        // Create a new configuration request and add two rules
                        CORSConfiguration configuration = new CORSConfiguration
                        {
                            Rules = new System.Collections.Generic.List <CORSRule>
                            {
                                new CORSRule
                                {
                                    Id             = "CORSRule1",
                                    AllowedMethods = new List <string> {
                                        "PUT", "POST", "DELETE"
                                    },
                                    AllowedOrigins = new List <string> {
                                        "http://*.example.com"
                                    }
                                },
                                new CORSRule
                                {
                                    Id             = "CORSRule2",
                                    AllowedMethods = new List <string> {
                                        "GET"
                                    },
                                    AllowedOrigins = new List <string> {
                                        "*"
                                    },
                                    MaxAgeSeconds = 3000,
                                    ExposeHeaders = new List <string> {
                                        "x-amz-server-side-encryption"
                                    }
                                }
                            }
                        };

                        // Add the configuration to the bucket
                        PutCORSConfiguration(configuration);

                        // Retrieve an existing configuration
                        configuration = GetCORSConfiguration();

                        // Add a new rule.
                        configuration.Rules.Add(new CORSRule
                        {
                            Id             = "CORSRule3",
                            AllowedMethods = new List <string> {
                                "HEAD"
                            },
                            AllowedOrigins = new List <string> {
                                "http://www.example.com"
                            }
                        });

                        // Add the configuration to the bucket
                        PutCORSConfiguration(configuration);

                        // Verify that there are now three rules
                        configuration = GetCORSConfiguration();
                        Console.WriteLine();
                        Console.WriteLine("Expected # of rulest=3; found:{0}", configuration.Rules.Count);
                        Console.WriteLine();
                        Console.WriteLine("Pause before configuration delete. To continue, click Enter...");
                        Console.ReadKey();

                        // Delete the configuration
                        DeleteCORSConfiguration();

                        // Retrieve a nonexistent configuration
                        configuration = GetCORSConfiguration();
                        Debug.Assert(configuration == null);
                    }

                    Console.WriteLine("Example complete.");
                }
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString());
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
                Console.ReadKey();
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            String photo = @"C:\Temp\banner.png";

            Image image = new Image();

            try
            {
                using (FileStream fs = new FileStream(photo, FileMode.Open, FileAccess.Read))
                {
                    byte[] data = null;
                    data = new byte[fs.Length];
                    fs.Read(data, 0, (int)fs.Length);
                    image.Bytes = new MemoryStream(data);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to load file " + photo);
                return;
            }

            try
            {
                // Constructs a SharedCredentialsFile object from the default credentials file.
                SharedCredentialsFile sharedCredentialsFile = new SharedCredentialsFile();

                // Get the [default] profile from the credentials file.
                CredentialProfile defaultProfile = GetDefaultProfile(sharedCredentialsFile);

                if (defaultProfile != null)
                {
                    // Get the credentials (access key, secret access key, etc.)
                    AWSCredentials credentials = AWSCredentialsFactory.GetAWSCredentials(defaultProfile, new SharedCredentialsFile());

                    AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials, RegionEndpoint.USEast1);

                    DetectTextRequest detectTextRequest = new DetectTextRequest()
                    {
                        Image = image,
                    };

                    DetectTextResponse detectTextResponse = rekognitionClient.DetectText(detectTextRequest);
                    foreach (TextDetection td in detectTextResponse.TextDetections)
                    {
                        Console.WriteLine(td.DetectedText);
                    }
                }
                else
                {
                    Console.WriteLine("AWS [default] profile not found");
                }
            }
            catch (AmazonRekognitionException ex)
            {
                Console.WriteLine("AWS Rekognition ERROR: {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }

            Console.ReadLine();
        }
Beispiel #5
0
        public async Task <AWSCredentials> ResolveAWSCredentials(string?profileName, string?lastUsedProfileName = null)
        {
            async Task <AWSCredentials> Resolve()
            {
                var chain = new CredentialProfileStoreChain();

                if (!string.IsNullOrEmpty(profileName) && chain.TryGetAWSCredentials(profileName, out var profileCredentials) &&
                    // Skip checking CanLoadCredentials for AssumeRoleAWSCredentials because it might require an MFA token and the callback hasn't been setup yet.
                    (profileCredentials is AssumeRoleAWSCredentials || await CanLoadCredentials(profileCredentials)))
                {
                    _toolInteractiveService.WriteLine($"Configuring AWS Credentials from Profile {profileName}.");
                    return(profileCredentials);
                }

                if (!string.IsNullOrEmpty(lastUsedProfileName) &&
                    chain.TryGetAWSCredentials(lastUsedProfileName, out var lastUsedCredentials) &&
                    await CanLoadCredentials(lastUsedCredentials))
                {
                    _toolInteractiveService.WriteLine($"Configuring AWS Credentials with previous configured profile value {lastUsedProfileName}.");
                    return(lastUsedCredentials);
                }

                try
                {
                    var fallbackCredentials = FallbackCredentialsFactory.GetCredentials();

                    if (await CanLoadCredentials(fallbackCredentials))
                    {
                        _toolInteractiveService.WriteLine("Configuring AWS Credentials using AWS SDK credential search.");
                        return(fallbackCredentials);
                    }
                }
                catch (AmazonServiceException)
                {
                    // FallbackCredentialsFactory throws an exception if no credentials are found. Burying exception because if no credentials are found
                    // we want to continue and ask the user to select a profile.
                }

                var sharedCredentials = new SharedCredentialsFile();

                if (sharedCredentials.ListProfileNames().Count == 0)
                {
                    throw new NoAWSCredentialsFoundException("Unable to resolve AWS credentials to access AWS.");
                }

                var selectedProfileName = _consoleUtilities.AskUserToChoose(sharedCredentials.ListProfileNames(), "Select AWS Credentials Profile", null);

                if (chain.TryGetAWSCredentials(selectedProfileName, out var selectedProfileCredentials) &&
                    (await CanLoadCredentials(selectedProfileCredentials)))
                {
                    return(selectedProfileCredentials);
                }

                throw new NoAWSCredentialsFoundException($"Unable to create AWS credentials for profile {selectedProfileName}.");
            }

            var credentials = await Resolve();

            if (credentials is AssumeRoleAWSCredentials assumeRoleAWSCredentials)
            {
                var assumeOptions = assumeRoleAWSCredentials.Options;
                assumeOptions.MfaTokenCodeCallback = new AssumeRoleMfaTokenCodeCallback(_toolInteractiveService, _directoryManager, assumeOptions).Execute;
            }

            return(credentials);
        }
Beispiel #6
0
        /// <summary>
        /// Parses sleet.json to find the source and constructs it.
        /// </summary>
        public static async Task <ISleetFileSystem> CreateFileSystemAsync(LocalSettings settings, LocalCache cache, string source)
        {
            ISleetFileSystem result = null;

            var sources = settings.Json["sources"] as JArray;

            if (sources == null)
            {
                throw new ArgumentException("Invalid config. No sources found.");
            }

            foreach (var sourceEntry in sources.Select(e => (JObject)e))
            {
                var sourceName = JsonUtility.GetValueCaseInsensitive(sourceEntry, "name");

                if (source.Equals(sourceName, StringComparison.OrdinalIgnoreCase))
                {
                    var path          = JsonUtility.GetValueCaseInsensitive(sourceEntry, "path");
                    var baseURIString = JsonUtility.GetValueCaseInsensitive(sourceEntry, "baseURI");
                    var feedSubPath   = JsonUtility.GetValueCaseInsensitive(sourceEntry, "feedSubPath");
                    var type          = JsonUtility.GetValueCaseInsensitive(sourceEntry, "type")?.ToLowerInvariant();

                    string absolutePath;
                    if (path != null && type == "local")
                    {
                        if (settings.Path == null && !Path.IsPathRooted(NuGetUriUtility.GetLocalPath(path)))
                        {
                            throw new ArgumentException("Cannot use a relative 'path' without a sleet.json file.");
                        }

                        var nonEmptyPath = path == "" ? "." : path;

                        var absoluteSettingsPath = NuGetUriUtility.GetAbsolutePath(Directory.GetCurrentDirectory(), settings.Path);

                        var settingsDir = Path.GetDirectoryName(absoluteSettingsPath);
                        absolutePath = NuGetUriUtility.GetAbsolutePath(settingsDir, nonEmptyPath);
                    }
                    else
                    {
                        absolutePath = path;
                    }

                    var pathUri = absolutePath != null?UriUtility.EnsureTrailingSlash(UriUtility.CreateUri(absolutePath)) : null;

                    var baseUri = baseURIString != null?UriUtility.EnsureTrailingSlash(UriUtility.CreateUri(baseURIString)) : pathUri;

                    if (type == "local")
                    {
                        if (pathUri == null)
                        {
                            throw new ArgumentException("Missing path for account.");
                        }

                        result = new PhysicalFileSystem(cache, pathUri, baseUri);
                    }
                    else if (type == "azure")
                    {
                        var connectionString = JsonUtility.GetValueCaseInsensitive(sourceEntry, "connectionString");
                        var container        = JsonUtility.GetValueCaseInsensitive(sourceEntry, "container");

                        if (string.IsNullOrEmpty(connectionString))
                        {
                            throw new ArgumentException("Missing connectionString for azure account.");
                        }

                        if (connectionString.Equals(AzureFileSystem.AzureEmptyConnectionString, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new ArgumentException("Invalid connectionString for azure account.");
                        }

                        if (string.IsNullOrEmpty(container))
                        {
                            throw new ArgumentException("Missing container for azure account.");
                        }

                        var azureAccount = CloudStorageAccount.Parse(connectionString);

                        if (pathUri == null)
                        {
                            // Get the default url from the container
                            pathUri = AzureUtility.GetContainerPath(azureAccount, container);
                        }

                        if (baseUri == null)
                        {
                            baseUri = pathUri;
                        }

                        result = new AzureFileSystem(cache, pathUri, baseUri, azureAccount, container, feedSubPath);
                    }
#if !SLEETLEGACY
                    else if (type == "s3")
                    {
                        var profileName     = JsonUtility.GetValueCaseInsensitive(sourceEntry, "profileName");
                        var accessKeyId     = JsonUtility.GetValueCaseInsensitive(sourceEntry, "accessKeyId");
                        var secretAccessKey = JsonUtility.GetValueCaseInsensitive(sourceEntry, "secretAccessKey");
                        var bucketName      = JsonUtility.GetValueCaseInsensitive(sourceEntry, "bucketName");
                        var region          = JsonUtility.GetValueCaseInsensitive(sourceEntry, "region");
                        var serviceURL      = JsonUtility.GetValueCaseInsensitive(sourceEntry, "serviceURL");
                        var compress        = JsonUtility.GetBoolCaseInsensitive(sourceEntry, "compress", true);

                        if (string.IsNullOrEmpty(bucketName))
                        {
                            throw new ArgumentException("Missing bucketName for Amazon S3 account.");
                        }

                        if (string.IsNullOrEmpty(region) && string.IsNullOrEmpty(serviceURL))
                        {
                            throw new ArgumentException("Either 'region' or 'serviceURL' must be specified for an Amazon S3 account");
                        }
                        if (!string.IsNullOrEmpty(region) && !string.IsNullOrEmpty(serviceURL))
                        {
                            throw new ArgumentException("Options 'region' and 'serviceURL' cannot be used together");
                        }

                        AmazonS3Config config = null;
                        if (serviceURL != null)
                        {
                            config = new AmazonS3Config()
                            {
                                ServiceURL       = serviceURL,
                                ProxyCredentials = CredentialCache.DefaultNetworkCredentials
                            };
                        }
                        else
                        {
                            config = new AmazonS3Config()
                            {
                                RegionEndpoint   = RegionEndpoint.GetBySystemName(region),
                                ProxyCredentials = CredentialCache.DefaultNetworkCredentials
                            };
                        }

                        AmazonS3Client amazonS3Client = null;

                        // Load credentials from the current profile
                        if (!string.IsNullOrWhiteSpace(profileName))
                        {
                            var credFile = new SharedCredentialsFile();
                            if (credFile.TryGetProfile(profileName, out var profile))
                            {
                                amazonS3Client = new AmazonS3Client(profile.GetAWSCredentials(profileSource: null), config);
                            }
                            else
                            {
                                throw new ArgumentException($"The specified AWS profileName {profileName} could not be found. The feed must specify a valid profileName for an AWS credentials file. For help on credential files see: https://docs.aws.amazon.com/sdk-for-net/v2/developer-guide/net-dg-config-creds.html#creds-file");
                            }
                        }
                        // Load credentials explicitly with an accessKey and secretKey
                        else if (
                            !string.IsNullOrWhiteSpace(accessKeyId) &&
                            !string.IsNullOrWhiteSpace(secretAccessKey))
                        {
                            amazonS3Client = new AmazonS3Client(new BasicAWSCredentials(accessKeyId, secretAccessKey), config);
                        }
                        // Load credentials from Environment Variables
                        else if (
                            !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_ACCESSKEY)) &&
                            !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(EnvironmentVariablesAWSCredentials.ENVIRONMENT_VARIABLE_SECRETKEY)))
                        {
                            amazonS3Client = new AmazonS3Client(new EnvironmentVariablesAWSCredentials(), config);
                        }
                        // Load credentials from an ECS docker container
                        else if (
                            !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(ECSTaskCredentials.ContainerCredentialsURIEnvVariable)))
                        {
                            amazonS3Client = new AmazonS3Client(new ECSTaskCredentials(), config);
                        }
                        // Assume IAM role
                        else
                        {
                            using (var client = new AmazonSecurityTokenServiceClient(config.RegionEndpoint))
                            {
                                try
                                {
                                    var identity = await client.GetCallerIdentityAsync(new GetCallerIdentityRequest());
                                }
                                catch (Exception ex)
                                {
                                    throw new ArgumentException(
                                              "Failed to determine AWS identity - ensure you have an IAM " +
                                              "role set, have set up default credentials or have specified a profile/key pair.", ex);
                                }
                            }

                            amazonS3Client = new AmazonS3Client(config);
                        }

                        if (pathUri == null)
                        {
                            // Find the default path
                            pathUri = AmazonS3Utility.GetBucketPath(bucketName, config.RegionEndpoint.SystemName);
                        }

                        if (baseUri == null)
                        {
                            baseUri = pathUri;
                        }

                        result = new AmazonS3FileSystem(
                            cache,
                            pathUri,
                            baseUri,
                            amazonS3Client,
                            bucketName,
                            feedSubPath,
                            compress);
                    }
#endif
                }
            }

            return(result);
        }