Esempio n. 1
0
        /// <summary>
        /// The CreateKeyPair operation creates a new 2048 bit RSA key pair and returns a
        /// unique ID that can be used to reference this key pair when launching new
        /// instances. For more information, see RunInstances.
        /// 
        /// </summary>
        /// <param name="service">Instance of AmazonEC2 service</param>
        /// <param name="request">CreateKeyPairRequest request</param>
        public static void InvokeCreateKeyPair(AmazonEC2 service, CreateKeyPairRequest request)
        {
            try 
            {
                CreateKeyPairResponse response = service.CreateKeyPair(request);
                
                
                Console.WriteLine ("Service Response");
                Console.WriteLine ("=============================================================================");
                Console.WriteLine ();

                Console.WriteLine("        CreateKeyPairResponse");
                if (response.IsSetResponseMetadata())
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata  responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId())
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                }
                if (response.IsSetCreateKeyPairResult())
                {
                    Console.WriteLine("            CreateKeyPairResult");
                    CreateKeyPairResult  createKeyPairResult = response.CreateKeyPairResult;
                    if (createKeyPairResult.IsSetKeyPair())
                    {
                        Console.WriteLine("                KeyPair");
                        KeyPair  keyPair = createKeyPairResult.KeyPair;
                        if (keyPair.IsSetKeyName())
                        {
                            Console.WriteLine("                    KeyName");
                            Console.WriteLine("                        {0}", keyPair.KeyName);
                        }
                        if (keyPair.IsSetKeyFingerprint())
                        {
                            Console.WriteLine("                    KeyFingerprint");
                            Console.WriteLine("                        {0}", keyPair.KeyFingerprint);
                        }
                        if (keyPair.IsSetKeyMaterial())
                        {
                            Console.WriteLine("                    KeyMaterial");
                            Console.WriteLine("                        {0}", keyPair.KeyMaterial);
                        }
                    }
                }

            } 
            catch (AmazonEC2Exception ex) 
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Request ID: " + ex.RequestId);
                Console.WriteLine("XML: " + ex.XML);
            }
        }
Esempio n. 2
0
        //once we get here we know the key file doesn't exist
        private void createKayPair()
        {
            try
            {
                string keyFileDir = CAwsConfig.getEc2BootstrapperDirectory();
                if (Directory.Exists(keyFileDir) == false)
                {
                    Directory.CreateDirectory(keyFileDir);
                }

                string keyFilePath = null;

                FolderBrowserDialog folder = new FolderBrowserDialog();
                folder.ShowNewFolderButton = true;
                folder.SelectedPath        = keyFileDir;
                folder.Description         = "Please select directory where you want to save key file";
                DialogResult result = DialogResult.No;
                while (result == DialogResult.No)
                {
                    if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        keyFilePath = folder.SelectedPath + "\\" + _keyPairName + ".pem";
                        if (File.Exists(keyFilePath))
                        {
                            result = MessageBox.Show(null, "Key file " + keyFilePath + " exists. Do you want to overwrite it?", "Key File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                CreateKeyPairRequest request = new CreateKeyPairRequest();
                request.KeyName = _keyPairName;
                CreateKeyPairResponse response = _service.CreateKeyPair(request);
                if (response.IsSetCreateKeyPairResult())
                {
                    CreateKeyPairResult createKeyPairResult = response.CreateKeyPairResult;
                    if (createKeyPairResult.IsSetKeyPair())
                    {
                        using (FileStream stream = new FileStream(
                                   keyFilePath, FileMode.Create, FileAccess.Write))
                        {
                            KeyPair keyPair = createKeyPairResult.KeyPair;
                            if (keyPair.IsSetKeyMaterial())
                            {
                                byte[] fileData = new UTF8Encoding(true).GetBytes(keyPair.KeyMaterial);

                                stream.Write(fileData, 0, fileData.Length);
                                CAwsConfig.Instance.setKeyFilePath(_keyPairName, keyFilePath);
                                CAwsConfig.Instance.commit();
                            }
                        }
                    }
                }
            }
            catch (AmazonEC2Exception ex)
            {
                throw new Exception("Caught Exception: " + ex.XML);
            }
        }