private bool keyExistOnServer() { bool exist = false; CEc2Service serv = new CEc2Service(); List <string> kps = serv.descrbibeKeyPairs(); foreach (string kp in kps) { if (string.Compare(kp, _keyPairName) == 0) { exist = true; break; } } if (exist == true) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "PEM files (*.pem)|*.pem"; ofd.InitialDirectory = CAwsConfig.getEc2BootstrapperDirectory(); ofd.Title = "Select private key file for " + _keyPairName; if (System.Windows.Forms.DialogResult.OK == ofd.ShowDialog()) { CAwsConfig.Instance.setKeyFilePath(_keyPairName, ofd.FileName); CAwsConfig.Instance.commit(); } else { throw new Exception("key " + _keyPairName + " is not associated its key file."); } } return(exist); }
private void downloadAndInstallCertificate() { try { string ec2BootstrapperDir = CAwsConfig.getEc2BootstrapperDirectory(); if (Directory.Exists(ec2BootstrapperDir) == false) { Directory.CreateDirectory(ec2BootstrapperDir); } string instanceDir = ec2BootstrapperDir + "\\" + _instanceId; if (Directory.Exists(instanceDir) == false) { Directory.CreateDirectory(instanceDir); } string certFilePath = instanceDir + "\\" + jwCertFile; if (File.Exists(certFilePath) == false) { string req = "http://" + _publicDns + "/" + jwCertFile; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req); request.Method = "GET"; request.ContentType = "text/xml"; // Get the response. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // Get the stream containing content returned by the server. using (Stream dataStream = response.GetResponseStream()) { // Open the stream using a StreamReader for easy access. using (StreamReader reader = new StreamReader(dataStream)) { // Read the content, which is the public key of the server string responseFromServer = reader.ReadToEnd(); TextWriter tw = new StreamWriter(certFilePath); tw.Write(responseFromServer); tw.Close(); } } } } if (File.Exists(certFilePath) == true) { //install certificate installCertificate(certFilePath); } } catch (Exception ex) { throw new Exception("DownloadAndInstallCertificate fails." + ex.Message); } }
//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); } }