繁体   English   中英

在 Google Cloud Platform 中,我无法使用 .net SDK 设置元数据并获取 windows VM 密码

In Google Cloud Platform I can`t set metadata and get windows VM password using .net SDK

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我正在使用 .NET SDK 并想获得 windows 密码。

我已阅读此文档并根据Java示例编写代码。

但是,我在执行SetMetadataRequest时在串口 1 上遇到了这个错误,我无法在串口 4 上获取加密密码。

-------设置元数据时的错误消息------------------------- ------

2019/09/16 12:28:36 GCEWindowsAgent:错误帐户。go:275:无效字符 ':' 在顶级值之后”

我的代码是:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Compute.v1;
using Google.Apis.Compute.v1.Data;
using Google.Apis.Services;
using java.math;
using java.security;
using java.security.spec;
using java.util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace GoogleHelper
{
    internal class GoogleVmHelper : VmTaskInterface
    {

        private ComputeService _Client;

        public async Task<bool> SetMetadata()
        {

// //---after instantiate of ComputeService _Client////

            InstancesResource.GetRequest describeServerReq = _Client.Instances.Get("projectid", "region", "serverid");
            Data.Instance describeServerRes = await describeServerReq.ExecuteAsync();
            Metadata oldMetaData = describeServerRes.Metadata;

            // Generate the public/private key pair for encryption and decryption.
            KeyPair keys = generateKeys();
            var buildedKeys = buildKeyMetadata(keys);

            //Replace Metadata
            string newItemString = buildedKeys.ToString();

            // Get the list containing all of the Metadata entries for this instance.
            var items = oldMetaData.Items;

            // If the instance has no metadata, items can be returned as null.
            if (items == null)
            {
                items = new List<Metadata.ItemsData>();
                oldMetaData.Items = items;
            }

            // Find the "windows-keys" entry and update it.
            bool isFound = false;
            foreach (var item in items)
            {
                if (item.Key == "windows-keys")
                {
                    // Replace item's value with the new entry.
                    // To prevent race conditions, production code may want to maintain a
                    // list where the oldest entries are removed once the 32KB limit is
                    // reached for the metadata entry.
                    item.Value = newItemString;
                    isFound = true;
                    break;
                }
            }

            if (!isFound)
            {
                // "windows.keys" entry doesn't exist in the metadata - append it.
                // This occurs when running password-reset for the first time on an instance.
                var addItem = new Metadata.ItemsData();
                addItem.Key = "windows-keys";
                addItem.Value = newItemString;
                items.Add(addItem);
            }

            oldMetaData.Items = items;

            //Set Metadata
            var setMetadataReq = new InstancesResource.SetMetadataRequest(_Client, oldMetaData, _Profile.ProjectID, _Task.Region, _Task.ServerID);
            var serMEtaDataRes = await setMetadataReq.ExecuteAsync();

            await System.Threading.Tasks.Task.Delay(30000);

            //GetPassword
            var getSerialPortOutputReq = new InstancesResource.GetSerialPortOutputRequest(_Client, _Profile.ProjectID, _Task.Region, _Task.ServerID);
            getSerialPortOutputReq.Port = 4;
            var getSerialPortOutputRes = await getSerialPortOutputReq.ExecuteAsync();

            //EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
            var entries = getSerialPortOutputRes.Contents;
            //EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY

            return true;
        }


        private KeyPair generateKeys()
        {
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

            // Key moduli for encryption/decryption are 2048 bits long.
            keyGen.initialize(2048);

            return keyGen.genKeyPair();
        }

        private JObject jsonEncode(KeyPair keys)
        {
            KeyFactory factory = KeyFactory.getInstance("RSA");

            // Get the RSA spec for key manipulation.
            RSAPublicKeySpec pubSpec = factory.getKeySpec(keys.getPublic(), typeof(RSAPublicKeySpec)) as RSAPublicKeySpec;

            // Extract required parts of the key.
            BigInteger modulus = pubSpec.getModulus();
            BigInteger exponent = pubSpec.getPublicExponent();

            // Strip out the leading 0 byte in the modulus.
            byte[] arr = Arrays.copyOfRange(modulus.toByteArray(), 1, modulus.toByteArray().Length);

            JObject returnJson = new JObject();

            // Encode the modulus, add to returned JSON object.
            String modulusString = Convert.ToBase64String(arr).Replace("\n", "");
            returnJson.Add("modulus", modulusString);

            // Encode exponent, add to returned JSON object.
            String exponentString = Convert.ToBase64String(exponent.toByteArray()).Replace("\n", "");

            returnJson.Add("exponent", exponentString);

            return returnJson;
        }


        private JObject buildKeyMetadata(KeyPair pair)
        {
            // Encode the public key into the required JSON format.
           var metadataValues =  jsonEncode(pair);

            // Add username and email.
            metadataValues.Add("userName", "myusername");
            metadataValues.Add("email", "myemal");

            //Create the date on which the new keys expire.
            //DateTime now = new DateTime();
            var expiredDate = DateTime.Now + TimeSpan.FromMinutes(5);

            //Format the date to match rfc3339.
            DateTime utcTime = System.TimeZoneInfo.ConvertTimeToUtc(expiredDate);
            String dateString = utcTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'");
            //Encode the expiration date for the returned JSON dictionary.
            metadataValues.Add("expireOn", dateString);

            return metadataValues;
        }

    }    
}

问题暂未有回复.您可以查看右边的相关问题.
9 google-cloud-storage上的图片尺寸元数据。

我需要知道要保存的图像的尺寸,以便可以将它们添加到og:image:width和og:image:height元标记中,以便通过Facebook,Twitter等进行图像预览- 如本Facebook文档所述。 如果我将这些图像保存到Google存储桶中,是否已经存在使用现有标准元数据查找这 ...

10 Oauth2 - 获取访问令牌,服务器以访问 Google-Cloud-platform

google 的 Oauth2 文档不包含(我已经看到)关于我正在尝试做什么的信息。 我想从我的服务器向我的谷歌云发布请求以获得响应。 为此,我需要一个访问令牌,目前我正在从操场手动获取它。 我将从我的服务器转到我的云,文档说明了有关用户接受访问然​​后被重定向等的信息,但我不涉及任何用户。 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM