Beispiel #1
0
        /// <summary>
        /// Checks the credential file for the presence of the s3 regional flag
        /// </summary>
        /// <returns>A nullable of S3UsEast1RegionalEndpointValue</returns>
        private static S3UsEast1RegionalEndpointValue?CheckCredentialsFile()
        {
            CredentialProfile profile;
            var profileName = Environment.GetEnvironmentVariable(AwsProfileEnvironmentVariable) ?? DefaultProfileName;

            credentialProfileChain.TryGetProfile(profileName, out profile);
            return(profile?.S3RegionalEndpoint);
        }
Beispiel #2
0
        public static AWSCredentials LoadProfile(string profileName)
        {
            CredentialProfileStoreChain chain = new CredentialProfileStoreChain();
            CredentialProfile           profile;

            chain.TryGetProfile(profileName, out profile);
            if (!chain.TryGetProfile(profileName, out profile))
            {
                chain.TryGetProfile("default", out profile);
            }

            Console.WriteLine($"Using Profile [{profile.Name}]");
            return(profile.GetAWSCredentials(null));
        }
Beispiel #3
0
        static async Task Main(string[] args)
        {
            var source = new CredentialProfileStoreChain();
            var creds  = source.TryGetProfile("personal", out var c) ? AWSCredentialsFactory.GetAWSCredentials(c, source) : null;
            var s3     = new AmazonS3Client(creds, RegionEndpoint.EUWest2);

            var resultsStorage = new S3ResultStorage(s3, "selectquery-dev-resultsbucket-cmlezfu231g");

            var underlyingExecutor = new S3SelectExecutor(s3, new InputSerialization
            {
                JSON = new JSONInput {
                    JsonType = JsonType.Lines
                },
                CompressionType = CompressionType.Gzip
            });
            var worker = new Worker(underlyingExecutor, resultsStorage);

            var sourceResolver = new S3SourceResolver(s3);
            var workerExecutor = new LocalWorkerExecutor(worker);

            var distributor = new Distributor(sourceResolver, workerExecutor, resultsStorage, resultsStorage);

            await distributor.QueryAsync(new DistributorInput(
                                             ParseQuery("SELECT * FROM s3object s LIMIT 100"),
                                             new DataSource.List(new[]
            {
                new Uri("s3://selectquery-data/0ECr0RR7ADAmZ7B6.gz"),
            })
                                             ));
        }
        public AppConfigAWSCredentials()
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;
            var logger = Logger.GetLogger(typeof(AppConfigAWSCredentials));

            // Attempt hardcoded key credentials first, then look for an explicit profile name
            // in either the SDK credential store or the shared credentials file. When using a profile
            // name, if a location is not given the search will use the default locations and name for
            // the credential file (assuming the profile is not found in the SDK store first)
            if (!string.IsNullOrEmpty(appConfig[ACCESSKEY]) && !string.IsNullOrEmpty(appConfig[SECRETKEY]))
            {
                var accessKey = appConfig[ACCESSKEY];
                var secretKey = appConfig[SECRETKEY];
                this._wrappedCredentials = new BasicAWSCredentials(accessKey, secretKey);
                logger.InfoFormat("Credentials found with {0} and {1} app settings", ACCESSKEY, SECRETKEY);
            }
            else if (!string.IsNullOrEmpty(AWSConfigs.AWSProfileName))
            {
                CredentialProfileStoreChain chain = new CredentialProfileStoreChain(AWSConfigs.AWSProfilesLocation);
                CredentialProfile           profile;
                if (chain.TryGetProfile(AWSConfigs.AWSProfileName, out profile))
                {
                    // Will throw a descriptive exception if profile.CanCreateAWSCredentials is false.
                    _wrappedCredentials = profile.GetAWSCredentials(profile.CredentialProfileStore, true);
                }
            }

            if (this._wrappedCredentials == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                  "The app.config/web.config files for the application did not contain credential information"));
            }
        }
Beispiel #5
0
        static Context GetContext(Config config, bool login = true)
        {
            var store = new CredentialProfileStoreChain();

            if (!store.TryGetProfile(config.Profile, out var profile))
            {
                throw new Exception($"Unable to find '{config.Profile}' AWS profile");
            }

            var credentials = profile.GetAWSCredentials(store);
            var region      = RegionEndpoint.GetBySystemName(config.Region);

            var result = new Context
            {
                Config              = config,
                Region              = region,
                AwsCredentials      = credentials,
                JsonStackSerializer = new JsonStackSerializer()
            };

            if (login)
            {
                result.Cloudformation = new AmazonCloudFormationClient(credentials, region);
                result.S3             = new AmazonS3Client(credentials, region);
                result.SSM            = new AmazonSimpleSystemsManagementClient(credentials, region);
                result.Lambda         = new AmazonLambdaClient(credentials, region);
            }

            return(result);
        }
Beispiel #6
0
        private static (AWSCredentials Creds, RegionEndpoint Region) GetAWSConfig(string profileName = null)
        {
            if (!string.IsNullOrEmpty(profileName))
            {
                var            chain = new CredentialProfileStoreChain();
                AWSCredentials creds;
                RegionEndpoint region;
                if (chain.TryGetProfile(profileName, out var profile))
                {
                    region = profile.Region;
                }
                else
                {
                    throw new ArgumentException($"No region profile with the name '{profileName}' was found.");
                }

                if (chain.TryGetAWSCredentials(profileName, out var credentials))
                {
                    creds = credentials;
                }
                else
                {
                    throw new ArgumentException($"No credential profile with credentials found with the name '{profileName}'.");
                }
            }

            return(FallbackCredentialsFactory.GetCredentials(), FallbackRegionFactory.GetRegionEndpoint());
        }
Beispiel #7
0
        public static AWSCredentials GetCredential(string profileName)
        {
            var chain = new CredentialProfileStoreChain();

            if (chain.TryGetProfile(profileName, out var profile) && chain.TryGetAWSCredentials(profileName, out var credentials))
            {
                return(credentials);
            }
            throw new NullReferenceException($"{nameof(profileName)} not found from exsiting profile list. Make sure you have set Profile");
        }
        /// <summary>
        /// Checks the credential file for the presence of the s3 regional flag
        /// </summary>
        /// <returns>A nullable of S3UsEast1RegionalEndpointValue</returns>
        private static S3UsEast1RegionalEndpointValue?CheckCredentialsFile()
        {
#if BCL || NETSTANDARD
            CredentialProfile profile;
            var profileName = Environment.GetEnvironmentVariable(AwsProfileEnvironmentVariable) ?? DefaultProfileName;
            credentialProfileChain.TryGetProfile(profileName, out profile);
            return(profile?.S3RegionalEndpoint);
#else
            return(null);
#endif
        }
Beispiel #9
0
        public static CredentialProfile GetCredentialProfile(string profileName = "")
        {
            if (string.IsNullOrWhiteSpace(profileName))
            {
                profileName = "default";
            }
            CredentialProfile credentialProfile = null;

            CredentialProfileStoreChain chain = new CredentialProfileStoreChain();

            chain.TryGetProfile(profileName, out credentialProfile);

            return(credentialProfile);
        }
Beispiel #10
0
    public static AwsStorageOptions BindConfiguration(this AwsStorageOptions options, IShellConfiguration shellConfiguration)
    {
        var section = shellConfiguration.GetSection("OrchardCore_Media_AmazonS3");

        if (section == null)
        {
            return(options);
        }

        options.BucketName   = section.GetValue(nameof(options.BucketName), String.Empty);
        options.BasePath     = section.GetValue(nameof(options.BasePath), String.Empty);
        options.CreateBucket = section.GetValue(nameof(options.CreateBucket), false);

        var credentials = section.GetSection("Credentials");

        if (credentials.Exists())
        {
            options.Credentials = new AwsStorageCredentials
            {
                RegionEndpoint =
                    credentials.GetValue(nameof(options.Credentials.RegionEndpoint), RegionEndpoint.USEast1.SystemName),
                SecretKey   = credentials.GetValue(nameof(options.Credentials.SecretKey), String.Empty),
                AccessKeyId = credentials.GetValue(nameof(options.Credentials.AccessKeyId), String.Empty),
            };
        }
        else
        {
            // Attempt to load Credentials from Profile.
            var profileName = section.GetValue("ProfileName", String.Empty);
            if (!String.IsNullOrEmpty(profileName))
            {
                var chain = new CredentialProfileStoreChain();
                if (chain.TryGetProfile(profileName, out var basicProfile))
                {
                    var awsCredentials = basicProfile.GetAWSCredentials(chain)?.GetCredentials();
                    if (awsCredentials != null)
                    {
                        options.Credentials = new AwsStorageCredentials
                        {
                            RegionEndpoint = basicProfile.Region.SystemName ?? RegionEndpoint.USEast1.SystemName,
                            SecretKey      = awsCredentials.SecretKey,
                            AccessKeyId    = awsCredentials.AccessKey
                        };
                    }
                }
            }
        }

        return(options);
    }
Beispiel #11
0
        public static (AWSCredentials credentials, RegionEndpoint region) GetAwsCredentials()
        {
            //Try to get the details out of the credential store chain
            var credentialChain = new CredentialProfileStoreChain();

            if (credentialChain.TryGetAWSCredentials("default", out var credentials) &&
                credentialChain.TryGetProfile("default", out var profile))
            {
                return(credentials, profile.Region);
            }

            //if not, can we grab them from environment variables - will throw
            return(new EnvironmentVariablesAWSCredentials(), new EnvironmentVariableAWSRegion().Region);
        }
Beispiel #12
0
        public static AmazonS3Client CreateClient(string profileName)
        {
            const string errorMessage = "Failed to load AWS credential profile, '{0}'({1}). If not registered profile, run cflogget.exe register ... ";

            var chain = new CredentialProfileStoreChain();

            if (!chain.TryGetAWSCredentials(profileName, out AWSCredentials awsCredentials))
            {
                throw new ApplicationException(string.Format(errorMessage, profileName, "TryGetAWSCredentials"));
            }
            if (!chain.TryGetProfile(profileName, out CredentialProfile profile))
            {
                throw new ApplicationException(string.Format(errorMessage, profileName, "TryGetProfile"));
            }

            return(new AmazonS3Client(awsCredentials, profile.Region));
        }
Beispiel #13
0
 private void SetRegionFromProfile()
 {
     try
     {
         var            myCredentialPRofileStoreChain = new CredentialProfileStoreChain();
         AWSCredentials myCredentials;
         myCredentialPRofileStoreChain.TryGetAWSCredentials(cboAWSExistingProfiles.Text, out myCredentials);
         CredentialProfile myProfile;
         myCredentialPRofileStoreChain.TryGetProfile(cboAWSExistingProfiles.Text, out myProfile);
         txtAWSShowRegion.Text = myProfile.Region.ToString();
     }
     catch (AmazonS3Exception s3Exception)
     {
         MessageBox.Show(s3Exception.Message,
                         s3Exception.InnerException.Message);
     }
 }
        private AWSCredentials GetAwsCredentials()
        {
            CredentialProfileStoreChain credentialProfileStoreChain = new CredentialProfileStoreChain();

            CredentialProfile profile;

            if (credentialProfileStoreChain.TryGetProfile(_settings.ProfileName, out profile))
            {
                AWSCredentials awsCredentials;
                if (AWSCredentialsFactory.TryGetAWSCredentials(profile, null, out awsCredentials))
                {
                    return(awsCredentials);
                }
            }

            // Production. Uses roles.
            return(null);
        }
Beispiel #15
0
        public MoviesController(_300910377_KAUR__300916412_YANG__Lab2Context context, IConfiguration conf)
        {
            _context = context;

            bucketName = conf.GetSection("AWS").GetSection("BucketName").Value;

            var awsOptions  = conf.GetAWSOptions();
            var profileName = awsOptions.Profile;

            CredentialProfile           credentialProfile;
            AWSCredentials              aWSCredentials;
            CredentialProfileStoreChain chain = new CredentialProfileStoreChain();

            chain.TryGetAWSCredentials(profileName, out aWSCredentials);
            chain.TryGetProfile(profileName, out credentialProfile);


            s3Client = new AmazonS3Client(aWSCredentials, awsOptions.Region);
        }
        public IAmazonS3 CreateS3Client()
        {
            var            config = AwsConfigFactory.GetS3Config();
            RegionEndpoint region = config.GetRegionEndpoint();

            CredentialProfileStoreChain credentialProfileStoreChain = new CredentialProfileStoreChain();

            CredentialProfile profile;

            if (credentialProfileStoreChain.TryGetProfile(config.ProfileName, out profile))
            {
                AWSCredentials awsCredentials;
                if (AWSCredentialsFactory.TryGetAWSCredentials(profile, null, out awsCredentials))
                {
                    return(new AmazonS3Client(awsCredentials, region));
                }
            }

            // Production. Uses roles.
            return(new AmazonS3Client(region));
        }
Beispiel #17
0
        private void TestRequesterPays(string profileName, RequestPayer requestPayer, bool useSigV4)
        {
            var originalUseSigV4 = AWSConfigsS3.UseSignatureVersion4;

            try
            {
                AWSConfigsS3.UseSignatureVersion4 = useSigV4;

                CredentialProfile profile;
                var credsFile = new CredentialProfileStoreChain();
                if (credsFile.TryGetProfile(profileName, out profile))
                {
                    var credentials = AWSCredentialsFactory.GetAWSCredentials(profile, credsFile);

                    var client = new AmazonS3Client(credentials, Region);
                    var url    = client.GetPreSignedURL(new GetPreSignedUrlRequest
                    {
                        BucketName   = BucketName,
                        Key          = Key,
                        Expires      = DateTime.Now.AddMinutes(20),
                        RequestPayer = requestPayer
                    });

                    var wc = new WebClient();
                    Assert.AreEqual(wc.DownloadString(url), Content);
                }
                else
                {
                    throw new ArgumentException("The profile " + profileName + "does not exist.");
                }
            }
            finally
            {
                AWSConfigsS3.UseSignatureVersion4 = originalUseSigV4;
            }
        }
Beispiel #18
0
        private void btnUploadFile_Click(object sender, EventArgs e)
        {
            String         strBucketName     = txtAWSS3BucketName.Text;
            String         strKeyName        = txtAWSS3KeyName.Text;
            String         strFilePath       = txtFilePath.Text;
            S3StorageClass scAWSStorageClass = cboAWSS3StorageClass.Text;
            Int64          intFileSize       = new FileInfo(strFilePath).Length;
            DateTime       dtStartDateTime   = DateTime.Now;
            Int64          intUploadDuration;
            String         strProgress;
            S3StorageClass strS3StorageClass = cboAWSS3StorageClass.Text.Length != 0 ? S3StorageClass.FindValue(cboAWSS3StorageClass.Text) : S3StorageClass.Standard;

            txtProgress.Visible = true;
            progressBar.Visible = true;

            try
            {
                var            myCredentialPRofileStoreChain = new CredentialProfileStoreChain();
                AWSCredentials myCredentials;
                myCredentialPRofileStoreChain.TryGetAWSCredentials(cboAWSExistingProfiles.Text, out myCredentials);
                CredentialProfile myProfile;
                myCredentialPRofileStoreChain.TryGetProfile(cboAWSExistingProfiles.Text, out myProfile);

                TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(myCredentials, myProfile.Region));
                intUploadDuration = (DateTime.Now - dtStartDateTime).Seconds;
                strProgress       = String.Format("Progress: 5%. Elapsed Time: {0} seconds.", intUploadDuration.ToString());
                txtProgress.Text  = strProgress;
                progressBar.Value = 5;

                if (intFileSize <= intMultiFileUpload && cboAWSS3StorageClass.Text == "STANDARD")
                {
                    if (txtAWSS3KeyName.Text.Length == 0)
                    {
                        strProgress       = "Progress: 10%.";
                        txtProgress.Text  = strProgress;
                        progressBar.Value = 10;
                        // 1. Upload a file, file name is used as the object key name.
                        fileTransferUtility.Upload(strFilePath, strBucketName);
                    }
                    else
                    {
                        strProgress       = "Progress: 10%.";
                        txtProgress.Text  = strProgress;
                        progressBar.Value = 10;
                        // 2. Specify object key name explicitly.
                        fileTransferUtility.Upload(strFilePath,
                                                   strBucketName, strKeyName);
                    }
                }
                else
                {
                    strProgress       = "Progress: 10%.";
                    txtProgress.Text  = strProgress;
                    progressBar.Value = 10;
                    // 4.Specify advanced settings/options.
                    TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
                    {
                        BucketName   = strBucketName,
                        FilePath     = strFilePath,
                        StorageClass = scAWSStorageClass,
                        PartSize     = intMultiFileUpload, //5368709120 is 5 GB, but we are going to set it to 5000000000 just to be on the safe side.  The original test was run as 6291456, // 6 MB.
                        Key          = strKeyName,
                        CannedACL    = S3CannedACL.PublicRead
                    };
                    fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
                    fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
                    fileTransferUtility.Upload(fileTransferUtilityRequest);
                }

                intUploadDuration = (DateTime.Now - dtStartDateTime).Minutes == 0 ? (DateTime.Now - dtStartDateTime).Seconds : (DateTime.Now - dtStartDateTime).Minutes;
                if (intUploadDuration > 60)
                {
                    strProgress = String.Format("Progress: 100%. Elapsed Time: {0} hour {1} minutes.", (intUploadDuration / 60).ToString(), (intUploadDuration % 60).ToString());
                }
                else
                {
                    if ((DateTime.Now - dtStartDateTime).Minutes == 0)
                    {
                        strProgress = String.Format("Progress: 100%. Elapsed Time: {0} seconds.", intUploadDuration.ToString());
                    }
                    else
                    {
                        strProgress = String.Format("Progress: 100%. Elapsed Time: {0} minutes.", intUploadDuration.ToString());
                    }
                }
                txtProgress.Text  = strProgress;
                progressBar.Value = 100;
            }
            catch (AmazonS3Exception s3Exception)
            {
                MessageBox.Show(s3Exception.Message,
                                s3Exception.InnerException.Message);
            }
            btnCancel.Text = "&Close";
        }
        public SQSBufferedConsumerTests()
        {
            //Must have credentials stored in the SDK Credentials store or shared credentials file
            var credentialChain = new CredentialProfileStoreChain();

            if (credentialChain.TryGetAWSCredentials("default", out var credentials) && credentialChain.TryGetProfile("default", out var profile))
            {
                var awsConnection = new AWSMessagingGatewayConnection(credentials, profile.Region);

                ChannelFactory channelFactory = new ChannelFactory(awsConnection, new SqsMessageConsumerFactory(awsConnection));
                var            name           = Guid.NewGuid().ToString();

                //we need the channel to create the queues and notifications
                channelFactory.CreateChannel(new Connection <MyCommand>(
                                                 name: new ConnectionName(name),
                                                 channelName: new ChannelName(name),
                                                 routingKey: new RoutingKey(_topicName),
                                                 bufferSize: BUFFER_SIZE
                                                 ));

                //we want to access via a consumer, to receive multiple messages - we don't want to expose on channel
                //just for the tests, so create a new consumer from the properties
                _consumer = new SqsMessageConsumer(awsConnection, new ChannelName(name).ToValidSQSQueueName(), BUFFER_SIZE);

                _messageProducer = new SqsMessageProducer(awsConnection);
            }
        }
Beispiel #20
0
        public static bool TryGetCredentials(
            this IAWSCredentialsArguments self,
            PSHost psHost,
            out AWSPSCredentials credentials,
            SessionState sessionState)
        {
            if (self == null)
            {
                throw new ArgumentNullException("self");
            }

            credentials = null;
            string name   = null;
            var    source = CredentialsSource.Unknown;
            var    userSpecifiedProfile = !string.IsNullOrEmpty(self.ProfileName);

            var profileChain = new CredentialProfileStoreChain(self.ProfileLocation);

            // we probe for credentials by first checking the bound parameters to see if explicit credentials
            // were supplied (keys, profile name, credential object), overriding anything in the shell environment
            if (AWSCredentialsFactory.TryGetAWSCredentials(
                    self.GetCredentialProfileOptions(),
                    profileChain,
                    out var innerCredentials))
            {
                source = CredentialsSource.Strings;
                name   = "Supplied Key Parameters";
                SetProxyAndCallbackIfNecessary(innerCredentials, self, psHost, sessionState);
            }

            // user gave us the profile name?
            if (innerCredentials == null && userSpecifiedProfile)
            {
                if (profileChain.TryGetProfile(self.ProfileName, out var credentialProfile))
                {
                    innerCredentials = AWSCredentialsFactory.GetAWSCredentials(credentialProfile, profileChain);
                    source           = CredentialsSource.Profile;
                    name             = self.ProfileName;
                    SetProxyAndCallbackIfNecessary(innerCredentials, self, psHost, sessionState);
                }
                else
                {
                    // if the user gave us an explicit profile name (and optional location) it's an error if we
                    // don't find it as otherwise we could drop through and pick up a 'default' profile that is
                    // for a different account
                    return(false);
                }
            }

            // how about an aws credentials object?
            if (innerCredentials == null && self.Credential != null)
            {
                innerCredentials = self.Credential;
                source           = CredentialsSource.CredentialsObject;
                name             = "Credentials Object";

                // don't set proxy and callback, use self.Credential as-is
            }

            // shell session variable set (this allows override of machine-wide environment variables)
            if (innerCredentials == null && sessionState != null)
            {
                if (TryGetAWSPSCredentialsFromConflictingType(
                        sessionState.PSVariable.GetValue(SessionKeys.AWSCredentialsVariableName),
                        out var psCredentials))
                {
                    credentials      = psCredentials;
                    source           = CredentialsSource.Session;
                    innerCredentials = credentials.Credentials; // so remaining probes are skipped

                    // don't set proxy and callback, use credentials.Credentials as-is
                }
            }

            // no explicit command-level or shell instance override set, start to inspect the environment
            // starting environment variables
            if (innerCredentials == null)
            {
                try
                {
                    var environmentCredentials = new EnvironmentVariablesAWSCredentials();
                    innerCredentials = environmentCredentials;
                    source           = CredentialsSource.Environment;
                    name             = "Environment Variables";

                    // no need to set proxy and callback - only basic or session credentials
                }
                catch
                {
                }
            }

            // get credentials from a 'default' profile?
            if (innerCredentials == null && !userSpecifiedProfile)
            {
                if (profileChain.TryGetProfile(SettingsStore.PSDefaultSettingName, out var credentialProfile) &&
                    credentialProfile.CanCreateAWSCredentials)
                {
                    innerCredentials = AWSCredentialsFactory.GetAWSCredentials(credentialProfile, profileChain);
                    source           = CredentialsSource.Profile;
                    name             = SettingsStore.PSDefaultSettingName;
                    SetProxyAndCallbackIfNecessary(innerCredentials, self, psHost, sessionState);
                }
            }

            // get credentials from a legacy default profile name?
            if (innerCredentials == null)
            {
                if (profileChain.TryGetProfile(SettingsStore.PSLegacyDefaultSettingName, out var credentialProfile) &&
                    credentialProfile.CanCreateAWSCredentials)
                {
                    if (AWSCredentialsFactory.TryGetAWSCredentials(
                            credentialProfile,
                            profileChain,
                            out innerCredentials))
                    {
                        source = CredentialsSource.Profile;
                        name   = SettingsStore.PSLegacyDefaultSettingName;
                        SetProxyAndCallbackIfNecessary(innerCredentials, self, psHost, sessionState);
                    }
                }
            }

            if (innerCredentials == null)
            {
                // try and load credentials from ECS endpoint (if the relevant environment variable is set)
                // or EC2 Instance Profile as a last resort
                try
                {
                    var relativeUri =
                        Environment.GetEnvironmentVariable(ECSTaskCredentials.ContainerCredentialsURIEnvVariable);
                    var fullUri = Environment.GetEnvironmentVariable(
                        ECSTaskCredentials.ContainerCredentialsFullURIEnvVariable);

                    if (!string.IsNullOrEmpty(relativeUri) || !string.IsNullOrEmpty(fullUri))
                    {
                        innerCredentials = new ECSTaskCredentials();
                        source           = CredentialsSource.Container;
                        name             = "Container";

                        // no need to set proxy and callback
                    }
                    else
                    {
                        innerCredentials = new InstanceProfileAWSCredentials();
                        source           = CredentialsSource.InstanceProfile;
                        name             = "Instance Profile";

                        // no need to set proxy and callback
                    }
                }
                catch
                {
                    innerCredentials = null;
                }
            }

            if (credentials == null && innerCredentials != null)
            {
                credentials = new AWSPSCredentials(innerCredentials, name, source);
            }

            return(credentials != null);
        }
Beispiel #21
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Txt_instance_name.Text))
            {
                MessageBox.Show("RDSインスタンス名を入れてください。", "エラー",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            //プロファイルから情報を読む
            var            chain = new CredentialProfileStoreChain();
            AWSCredentials awsCredentials;

            if (chain.TryGetAWSCredentials(AWS_PROFILE_NAME, out awsCredentials))
            {
                //ログ出力先に書き込めるかチェック
                if (!chk_write(log_base_path))
                {
                    return;
                }

                //読み込みOK
                DialogResult ret = MessageBox.Show("取得開始します。", "確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
                if (ret == DialogResult.Cancel)
                {
                    return;
                }
                button1.Enabled        = false;
                button2.Enabled        = false;
                Btn_Regist_Key.Enabled = false;
                Txt_msg.Text           = "ログデータ取得中!!";
                try
                {
                    CredentialProfile profile;
                    chain.TryGetProfile(AWS_PROFILE_NAME, out profile);

                    Aws_Util rds = new Aws_Util(awsCredentials, profile.Region);
                    rds.db_instance_identifier = Txt_instance_name.Text;
                    rds.log_base_path          = log_base_path;
                    rds.get_logs();

                    //問題なければ設定を保存する。
                    Properties.Settings.Default.last_save_path = log_base_path;
                    Properties.Settings.Default.instance_id    = Txt_instance_name.Text;

                    Properties.Settings.Default.Save();
                    Txt_msg.Text = "ログデータ取得完了!!";
                    MessageBox.Show("取得完了しました。");
                }
                catch (Amazon.RDS.Model.DBInstanceNotFoundException ex)
                {
                    MessageBox.Show("RDSインスタンスが存在しません:" + Environment.NewLine + ex.Message, "エラー",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    Txt_msg.Text = "システムエラー発生";
                }
                catch (Amazon.RDS.AmazonRDSException ex)
                {
                    MessageBox.Show("RDSインスタンス名が不正です。:" + Environment.NewLine + ex.Message, "エラー",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    Txt_msg.Text = "システムエラー発生";
                }
                finally
                {
                    button1.Enabled        = Enabled;
                    button2.Enabled        = Enabled;
                    Btn_Regist_Key.Enabled = Enabled;
                }
                return;
            }
            else
            {
                MessageBox.Show("AWSへ接続に必要なアクセスキー、シークレットキーを設定してください。");
                return;
            }
        }
        protected override void ProcessRecord()
        {
            AWSPSCredentials awsPSCredentialsFromCommandLine;
            RegionEndpoint   regionFromCommandLine;
            AWSPSCredentials awsPSCredentialsFromDefaultProfile;
            RegionEndpoint   regionFromDefaultProfile;
            AWSPSCredentials awsPSCredentialsFromPromptingUser = null;
            RegionEndpoint   regionFromPromptingUser           = null;

            // get what was passed in on the command line, and what's already in defaults
            GetFromCommandLine(out awsPSCredentialsFromCommandLine, out regionFromCommandLine);
            GetFromDefaultProfile(out awsPSCredentialsFromDefaultProfile, out regionFromDefaultProfile);

            // if the command line and defaults don't provide us what we need then prompt the user
            if (awsPSCredentialsFromCommandLine == null && awsPSCredentialsFromDefaultProfile == null)
            {
                awsPSCredentialsFromPromptingUser = PromptUserForCredentials();
            }

            if (regionFromCommandLine == null && regionFromDefaultProfile == null)
            {
                regionFromPromptingUser = PromptUserForRegion();
            }

            // figure out what needs to be saved to disk and what needs to get put in session
            var awsPSCredentialsToPersist      = awsPSCredentialsFromCommandLine == null ? awsPSCredentialsFromPromptingUser : awsPSCredentialsFromCommandLine;
            var awsPSCredentialsToPutInSession = awsPSCredentialsToPersist == null ? awsPSCredentialsFromDefaultProfile : awsPSCredentialsToPersist;

            var regionToPersist      = regionFromCommandLine == null ? regionFromPromptingUser : regionFromCommandLine;
            var regionToPutInSession = regionToPersist == null ? regionFromDefaultProfile : regionToPersist;

            // save credentials and region to disk if necessary
            if (awsPSCredentialsToPersist != null || regionToPersist != null)
            {
                if (string.Equals(AWSCredentialsArgumentsFullCmdlet.AWSCredentialsObjectSet, ParameterSetName, StringComparison.Ordinal))
                {
                    // We're storing from the -Credential parameter to a credentials file.
                    // Only some types of AWSCredentials are supported.
                    var options = CredentialProfileOptionsExtractor.ExtractProfileOptions(awsPSCredentialsToPersist.Credentials);
                    if (options != null)
                    {
                        SettingsStore.RegisterProfile(options, SettingsStore.PSDefaultSettingName, ProfileLocation, regionToPersist);
                    }
                }
                else
                {
                    if (string.Equals(AWSCredentialsArgumentsFullCmdlet.StoredProfileSet, ParameterSetName, StringComparison.Ordinal))
                    {
                        // We're copying from one profile to another.
                        var chain = new CredentialProfileStoreChain(ProfileLocation);
                        CredentialProfile profile;
                        if (chain.TryGetProfile(ProfileName, out profile))
                        {
                            profile.CredentialProfileStore.CopyProfile(ProfileName, SettingsStore.PSDefaultSettingName, true);
                            SettingsStore.RegisterProfile(new CredentialProfileOptions(), SettingsStore.PSDefaultSettingName, ProfileLocation, regionToPersist);
                        }
                        else
                        {
                            // Parameters.TryGetCredentials has already tested for this but...
                            this.ThrowTerminatingError(new ErrorRecord(
                                                           new ArgumentException("Cannot determine credentials from supplied parameters"),
                                                           "ArgumentException", ErrorCategory.InvalidArgument, this));
                        }
                    }
                    else
                    {
                        // We're storing from individual command line values to the default profile.
                        SettingsStore.RegisterProfile(GetCredentialProfileOptions(), SettingsStore.PSDefaultSettingName, ProfileLocation, regionToPersist);
                    }
                }

                if (string.IsNullOrEmpty(ProfileLocation))
                {
                    WriteVerbose("Updated SDK profile store.");
                }
                else
                {
                    WriteVerbose("Updated credential file at " + ProfileLocation);
                }
                WriteVerbose(string.Format("Default credentials and/or region have been stored to credentials profile '{0}' and set active for this shell.", SettingsStore.PSDefaultSettingName));
            }

            string scope = MyInvocation.BoundParameters.ContainsKey("Scope") ? Scope.ToString() + ":" : "";

            // put credentials and region in session
            this.SessionState.PSVariable.Set(scope + SessionKeys.AWSCredentialsVariableName, awsPSCredentialsToPutInSession);
            this.SessionState.PSVariable.Set(scope + SessionKeys.AWSRegionVariableName, regionToPutInSession.SystemName);
        }
Beispiel #23
0
        protected int InvokeTestSuite(string args, out string log)
        {
            var workingDir = WorkingDirectory.FullName;
            var file       = TestSuiteExecutable;
            var si         = new ProcessStartInfo
            {
                FileName               = file,
                Arguments              = args,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = true
            };

            Console.WriteLine("Invoking tests.");
            Console.WriteLine("File: {0}", file);
            Console.WriteLine("Args: {0}", args);
            Console.WriteLine("Dir:  {0}", workingDir);
            if (!string.IsNullOrWhiteSpace(TestExecutionProfile))
            {
                var chain = new CredentialProfileStoreChain();
                CredentialProfile profile;
                if (chain.TryGetProfile(TestExecutionProfile, out profile))
                {
                    Console.WriteLine($"Profile '{TestExecutionProfile}' found.");
                    si.EnvironmentVariables[AWS_PROFILE_ENVIRONMENT_VARIABLE] = TestExecutionProfile;
                    Console.WriteLine($"TestExecutionProfile: {TestExecutionProfile}");
                }
                else
                {
                    Console.WriteLine($"Profile '{TestExecutionProfile}' not found, ignoring the TestExecutionProfile attribute on test wrapper build task.");
                }
            }

            string error;
            string output;
            int    exitCode;

            using (var outWriter = new StringWriter())
                using (var errWriter = new StringWriter())
                    using (var p = new Process())
                    {
                        p.StartInfo           = si;
                        p.OutputDataReceived += LogMultiple(Console.Out, outWriter);
                        p.ErrorDataReceived  += LogMultiple(Console.Error, errWriter);
                        p.Start();
                        p.BeginOutputReadLine();
                        p.BeginErrorReadLine();

                        if (p.WaitForExit((int)MAX_SINGLE_EXEC_TIME.TotalMilliseconds))
                        {
                            p.WaitForExit();
                        }


                        output   = outWriter.ToString();
                        error    = errWriter.ToString();
                        exitCode = p.ExitCode;

                        if (!p.HasExited)
                        {
                            var separator = new string('-', 10);
                            using (var sw = new StringWriter())
                            {
                                sw.WriteLine("Process is stuck!");
                                sw.WriteLine("StdOut");
                                sw.WriteLine(separator);
                                sw.WriteLine(output);
                                sw.WriteLine(separator);
                                sw.WriteLine("ErrOut");
                                sw.WriteLine(separator);
                                sw.WriteLine(error);
                                sw.WriteLine(separator);
                                var message = sw.ToString();

                                try
                                {
                                    p.Kill();
                                }
                                catch (Exception) { }
                                throw new InvalidOperationException(message);
                            }
                        }

                        p.Close();
                    }

            using (var writer = new StringWriter())
            {
                writer.WriteLine(output);
                if (!string.IsNullOrEmpty(error))
                {
                    writer.WriteLine("StdErr:");
                    writer.WriteLine(error);
                }
                if (exitCode != 0)
                {
                    writer.WriteLine("Exit code = {0}", exitCode);
                }

                log = writer.ToString();
            }

            return(exitCode);
        }
        protected override void ProcessRecord()
        {
            AWSPSCredentials currentCredentials = null;

            if (this.TryGetCredentials(Host, out currentCredentials, SessionState))
            {
                WriteVerbose(InitializeDefaultConfigurationCmdlet.CredentialsSourceMessage(currentCredentials));

                if (string.IsNullOrEmpty(StoreAs))
                {
                    SetUpIfFederatedCredentials(currentCredentials);
                    string scope = MyInvocation.BoundParameters.ContainsKey("Scope") ? Scope.ToString() + ":" : "";
                    this.SessionState.PSVariable.Set(scope + SessionKeys.AWSCredentialsVariableName, currentCredentials);
                }
                else
                {
                    if (MyInvocation.BoundParameters.ContainsKey("Scope"))
                    {
                        this.ThrowTerminatingError(new ErrorRecord(
                                                       new ArgumentException("Parameters Scope and StoreAs cannot be used together."),
                                                       "ArgumentException", ErrorCategory.InvalidArgument, this));
                    }

                    if (string.Equals(AWSCredentialsArgumentsFullCmdlet.AWSCredentialsObjectSet, ParameterSetName, StringComparison.Ordinal))
                    {
                        // We're storing from the -Credential parameter to a credentials file.
                        // Only some types of AWSCredentials are supported.
                        var options = CredentialProfileOptionsExtractor.ExtractProfileOptions(currentCredentials.Credentials);
                        if (options != null)
                        {
                            SettingsStore.RegisterProfile(options, StoreAs, ProfileLocation, null);
                        }
                    }
                    else
                    {
                        if (string.Equals(AWSCredentialsArgumentsFullCmdlet.StoredProfileSet, ParameterSetName, StringComparison.Ordinal))
                        {
                            // We're copying from one profile to another.
                            var chain = new CredentialProfileStoreChain(ProfileLocation);
                            CredentialProfile profile;
                            if (chain.TryGetProfile(ProfileName, out profile))
                            {
                                profile.CredentialProfileStore.CopyProfile(ProfileName, StoreAs, true);
                            }
                            else
                            {
                                // Parameters.TryGetCredentials has already tested for this but...
                                this.ThrowTerminatingError(new ErrorRecord(
                                                               new ArgumentException("Cannot determine credentials from supplied parameters"),
                                                               "ArgumentException", ErrorCategory.InvalidArgument, this));
                            }
                        }
                        else
                        {
                            // We're storing from individual command line values to a credentials file.
                            SettingsStore.RegisterProfile(GetCredentialProfileOptions(), StoreAs, ProfileLocation, null);
                        }
                    }

                    if (string.IsNullOrEmpty(ProfileLocation))
                    {
                        WriteVerbose("Updated .NET credentials file.");
                    }
                    else
                    {
                        WriteVerbose("Updated shared credentials file at " + ProfileLocation);
                    }
                }
            }
            else
            {
                this.ThrowTerminatingError(new ErrorRecord(new ArgumentException("Cannot determine credentials from supplied parameters"),
                                                           "ArgumentException",
                                                           ErrorCategory.InvalidArgument,
                                                           this));
            }
        }
Beispiel #25
0
        public SqsQueuePurgeTests()
        {
            MyCommand myCommand = new MyCommand {
                Value = "Test"
            };

            _message = new Message(
                new MessageHeader(myCommand.Id, "MyCommand", MessageType.MT_COMMAND),
                new MessageBody(JsonConvert.SerializeObject((object)myCommand))
                );

            var credentialChain = new CredentialProfileStoreChain();

            if (credentialChain.TryGetAWSCredentials("default", out var credentials) && credentialChain.TryGetProfile("default", out var profile))
            {
                var awsConnection = new AWSMessagingGatewayConnection(credentials, profile.Region);

                _channelFactory = new ChannelFactory(awsConnection, new SqsMessageConsumerFactory(awsConnection));
                _channel        = _channelFactory.CreateChannel(_connection);

                _messageProducer = new SqsMessageProducer(awsConnection);
            }
        }
Beispiel #26
0
        static int Main(string[] args)
        {
            foreach (var line in titleBanner)
            {
                Console.WriteLine(line);
            }

            if (args == null || args.Length == 0)
            {
                // Help screen
                foreach (var line in helpScreen)
                {
                    Console.WriteLine(line);
                }
                System.Environment.Exit(-1);
            }

            Options options = null;

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed(o => options = o)
            .WithNotParsed(errors =>
            {
                foreach (var error in errors)
                {
                    Console.WriteLine(error);
                }
                System.Environment.Exit(-2);
            });

            // Setup AWS credentials
            var chain = new CredentialProfileStoreChain();

            AWSCredentials awsCredentials;
            RegionEndpoint awsRegion;

            if (!string.IsNullOrWhiteSpace(options.Profile))
            {
                if (!chain.TryGetAWSCredentials(options.Profile, out awsCredentials))
                {
                    Console.WriteLine($"Unable to retrieve credentials for profile {options.Profile}");
                    System.Environment.Exit(-3);
                    return(-3);
                }

                CredentialProfile credentialProfile;
                if (!chain.TryGetProfile(options.Profile, out credentialProfile))
                {
                    Console.WriteLine($"Unable to retrieve credential profile for {options.Profile}");
                    System.Environment.Exit(-4);
                    return(-4);
                }

                awsRegion = credentialProfile.Region ?? RegionEndpoint.GetBySystemName(options.Region);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(options.AccessKeyId))
                {
                    Console.Error.WriteLine("No profile was specified, but an access key ID was not provided either.");
                    System.Environment.Exit(-5);
                    return(-5);
                }

                if (string.IsNullOrWhiteSpace(options.AccessKeySecret))
                {
                    Console.Error.WriteLine("No profile was specified, but an access key secret was not provided either.");
                    System.Environment.Exit(-6);
                    return(-6);
                }

                awsCredentials = new BasicAWSCredentials(options.AccessKeyId, options.AccessKeySecret);
                awsRegion      = RegionEndpoint.GetBySystemName(options.Region);
            }

            var cts = new CancellationTokenSource();

            var getFindingsTask = Task.Run(new Func <Task <Tuple <object, Exception> > >(async() =>
            {
                var client = new AmazonGuardDutyClient(awsCredentials, awsRegion);

                var detectorRequest  = new ListDetectorsRequest();
                var detectorResponse = await client.ListDetectorsAsync(detectorRequest, cts.Token);

                dynamic bundle      = new ExpandoObject();
                bundle.type         = "bundle";
                bundle.id           = $"guardduty-stix-{DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture)}";
                bundle.spec_version = "2.0";

                var objects = new List <object>();

                foreach (var detectorId in detectorResponse.DetectorIds)
                {
                    var listFindingsRequest = new ListFindingsRequest()
                    {
                        DetectorId = detectorId,

                        /*FindingCriteria = new FindingCriteria
                         * {
                         *  Criterion = { { "service.archived", new Condition { Eq = { "FALSE" } } } }
                         * }*/
                    };

                    try
                    {
                        // Get list of findings
                        var listFindingsResponse = await client.ListFindingsAsync(listFindingsRequest, cts.Token);

                        // For the list, get the details
                        var getFindingsRequest = new GetFindingsRequest()
                        {
                            DetectorId = detectorId,
                            FindingIds = listFindingsResponse.FindingIds
                        };
                        var getFindingsResponse = await client.GetFindingsAsync(getFindingsRequest, cts.Token);

                        foreach (var finding in getFindingsResponse.Findings)
                        {
                            var sdo = await ConvertFindingToStixAsync(finding);
                            objects.Add(sdo);
                        }
                    }
                    catch (Exception e)
                    {
                        await Console.Error.WriteLineAsync(e.ToString());
                        return(new Tuple <object, Exception>(null, e));
                    }
                }

                bundle.objects = objects;
                return(new Tuple <object, Exception>(bundle, null));
            }));

            if (!Task.WaitAll(new[] { getFindingsTask }, 60000, cts.Token))
            {
                Console.Error.WriteLine("Failed to complete within 60 seconds, aborted.");
                System.Environment.Exit(-7);
                return(-7);
            }

            var result = getFindingsTask.Result;

            if (result.Item2 != null)
            {
                Console.Error.WriteLine($"Unable to parse output: {result.Item2.ToString()}");
                System.Environment.Exit(-8);
                return(-8);
            }

            if (string.IsNullOrWhiteSpace(options.OutputFile))
            {
                Console.Out.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result.Item1));
            }
            else
            {
                try
                {
                    using (var fs = new FileStream(options.OutputFile, FileMode.Create, FileAccess.Write))
                        using (var sw = new StreamWriter(fs))
                        {
                            sw.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result.Item1));
                        }

                    Console.Out.WriteLine($"Output saved to file {options.OutputFile}");
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine($"Unable to write file: {e.ToString()}");
                    System.Environment.Exit(-9);
                    return(-9);
                }
            }

            return(0);
        }
        public static (AWSCredentials credential, RegionEndpoint region) GetAWSCredentialsRegion(IPlugInContext context)
        {
            IConfiguration config         = context.Configuration;
            string         id             = config[ConfigConstants.ID];
            string         credentialRef  = config[ConfigConstants.CREDENTIAL_REF];
            string         accessKey      = config[ConfigConstants.ACCESS_KEY];
            string         secretKey      = config[ConfigConstants.SECRET_KEY];
            string         region         = ResolveConfigVariable(config[ConfigConstants.REGION]);
            string         profileName    = config[ConfigConstants.PROFILE_NAME];
            string         roleArn        = ResolveConfigVariable(config[ConfigConstants.ROLE_ARN]);
            AWSCredentials credential     = null;
            RegionEndpoint regionEndPoint = null;

            //Order 0: If the sink has credential providers, use if
            if (!string.IsNullOrWhiteSpace(credentialRef))
            {
                if (!(context.GetCredentialProvider(credentialRef) is ICredentialProvider <AWSCredentials> credentialProvider))
                {
                    throw new Exception($"Credential {credentialRef} not found or not an AWSCredential.");
                }
                credential = credentialProvider.GetCredentials();
            }
            // Order 1: If the sink has a profile entry, get the credential from profile store
            else if (!string.IsNullOrWhiteSpace(profileName))
            {
                CredentialProfileStoreChain credentialProfileChaine = new CredentialProfileStoreChain();
                if (credentialProfileChaine.TryGetAWSCredentials(profileName, out credential))
                {
                    if (credentialProfileChaine.TryGetProfile(profileName, out CredentialProfile profile))
                    {
                        regionEndPoint = profile.Region;
                    }
                }
                else
                {
                    throw new AmazonServiceException($"Profile name {profileName} not found.");
                }
            }
            // Order 2: If there is an accessKey, create the credential using accessKey and secretKey
            else if (!string.IsNullOrWhiteSpace(accessKey))
            {
                credential = new BasicAWSCredentials(accessKey, secretKey);
            }
            // Order 3: If nothing configured, using the Fallback credentials factory
            else
            {
                credential = FallbackCredentialsFactory.GetCredentials();
            }

            //If roleARN is specified. Assume if from the credential loaded above
            if (!string.IsNullOrWhiteSpace(roleArn))
            {
                ConfigureSTSRegionalEndpoint(config);
                credential = new AssumeRoleAWSCredentials(credential, roleArn, $"KinesisTap-{Utility.ComputerName}");
            }

            //Any region override?
            if (!string.IsNullOrWhiteSpace(region))
            {
                regionEndPoint = RegionEndpoint.GetBySystemName(region);
            }

            return(credential, regionEndPoint);
        }
Beispiel #28
0
        public SqsMessageProducerSendTests()
        {
            _myCommand = new MyCommand {
                Value = "Test"
            };
            _correlationId = Guid.NewGuid();
            _replyTo       = "http:\\queueUrl";
            _contentType   = "text\\plain";
            _topicName     = _myCommand.GetType().FullName.ToValidSNSTopicName();

            _message = new Message(
                new MessageHeader(_myCommand.Id, _topicName, MessageType.MT_COMMAND, _correlationId, _replyTo, _contentType),
                new MessageBody(JsonConvert.SerializeObject((object)_myCommand))
                );

            //Must have credentials stored in the SDK Credentials store or shared credentials file
            var credentialChain = new CredentialProfileStoreChain();

            if (credentialChain.TryGetAWSCredentials("default", out var credentials) && credentialChain.TryGetProfile("default", out var profile))
            {
                var awsConnection = new AWSMessagingGatewayConnection(credentials, profile.Region);

                _channelFactory = new ChannelFactory(awsConnection, new SqsMessageConsumerFactory(awsConnection));

                _channel = _channelFactory.CreateChannel(_connection);

                _messageProducer = new SqsMessageProducer(awsConnection);
            }
        }