Ejemplo n.º 1
0
        public static SamlToken GetSamlBearerToken(
            string ssoUrl, string ssoUserName, string ssoPassword)
        {
            var binding = VimAuthenticationHelper.GetCustomBinding();
            var address = new EndpointAddress(ssoUrl);

            var stsServiceClient =
                new STSService_PortTypeClient(binding, address);

            stsServiceClient.ClientCredentials.UserName.UserName = ssoUserName;
            stsServiceClient.ClientCredentials.UserName.Password = ssoPassword;

            RequestSecurityTokenType tokenType =
                new RequestSecurityTokenType();

            /**
             * For this request we need at least the following element in the
             * RequestSecurityTokenType set
             *
             * 1. Lifetime - represented by LifetimeType which specifies the
             * lifetime for the token to be issued
             *
             * 2. Tokentype - "urnoasisnamestcSAML20assertion", which is the
             * class that models the requested token
             *
             * 3. RequestType -
             * "httpdocsoasisopenorgwssxwstrust200512Issue", as we want
             * to get a token issued
             *
             * 4. KeyType -
             * "httpdocsoasisopenorgwssxwstrust200512Bearer",
             * representing the kind of key the token will have. There are two
             * options namely bearer and holder-of-key
             *
             * 5. SignatureAlgorithm -
             * "httpwwww3org200104xmldsigmorersasha256", representing the
             * algorithm used for generating signature
             *
             * 6. Renewing - represented by the RenewingType which specifies
             *  whether the token is renewable or not
             */
            tokenType.TokenType =
                TokenTypeEnum.urnoasisnamestcSAML20assertion;
            tokenType.RequestType =
                RequestTypeEnum.httpdocsoasisopenorgwssxwstrust200512Issue;
            tokenType.KeyType =
                KeyTypeEnum.httpdocsoasisopenorgwssxwstrust200512Bearer;
            tokenType.SignatureAlgorithm =
                SignatureAlgorithmEnum.httpwwww3org200104xmldsigmorersasha256;
            tokenType.Delegatable          = true;
            tokenType.DelegatableSpecified = true;

            LifetimeType       lifetime    = new LifetimeType();
            AttributedDateTime created     = new AttributedDateTime();
            String             createdDate = String.Format(dateFormat,
                                                           DateTime.Now.ToUniversalTime());

            created.Value    = createdDate;
            lifetime.Created = created;

            AttributedDateTime expires    = new AttributedDateTime();
            TimeSpan           duration   = new TimeSpan(1, 10, 10);
            String             expireDate = String.Format(dateFormat,
                                                          DateTime.Now.Add(duration).ToUniversalTime());

            expires.Value      = expireDate;
            lifetime.Expires   = expires;
            tokenType.Lifetime = lifetime;
            RenewingType renewing = new RenewingType();

            renewing.Allow     = false;
            renewing.OK        = true;
            tokenType.Renewing = renewing;

            RequestSecurityTokenResponseCollectionType responseToken =
                stsServiceClient.Issue(tokenType);
            RequestSecurityTokenResponseType rstResponse =
                responseToken.RequestSecurityTokenResponse;
            XmlElement samlTokenXml = rstResponse.RequestedSecurityToken;
            SamlToken  samlToken    = new SamlToken(samlTokenXml);

            return(samlToken);
        }
 public WaitForValues(VimAuthenticationHelper vimConnection)
 {
     this.vimConnection = vimConnection;
 }
        public static ManagedObjectReference GetCluster(string clusterName,
                                                        VimAuthenticationHelper vimConnection)
        {
            var vimPortType     = vimConnection.VimPortType;
            var serviceContent  = vimConnection.ServiceContent;
            var morefType       = "ClusterComputeResource";
            var morefProperties = new string[] { "name" };
            var objectContents  = new List <ObjectContent>();
            var containerView   = vimPortType.CreateContainerView(
                new CreateContainerViewRequest(
                    serviceContent.viewManager,
                    serviceContent.rootFolder,
                    new string[] { morefType },
                    true));

            var spec = new PropertyFilterSpec();

            spec.propSet                 = new PropertySpec[] { new PropertySpec() };
            spec.propSet[0].all          = morefProperties == null || morefProperties.Length == 0;
            spec.propSet[0].allSpecified = spec.propSet[0].all;
            spec.propSet[0].type         = morefType;
            spec.propSet[0].pathSet      = morefProperties;
            spec.objectSet               = new ObjectSpec[] { new ObjectSpec() };

            var ts = new TraversalSpec();

            ts.name = "view";
            ts.path = "view";
            ts.skip = false;
            ts.type = "ContainerView";
            spec.objectSet[0].obj       = containerView.returnval;
            spec.objectSet[0].selectSet = new SelectionSpec[] { ts };

            var result = vimPortType.RetrievePropertiesEx(
                new RetrievePropertiesExRequest(
                    serviceContent.propertyCollector,
                    new PropertyFilterSpec[] { spec },
                    new RetrieveOptions()));

            if (result != null)
            {
                var token = result.returnval.token;
                objectContents.AddRange(result.returnval.objects);
                while (!string.IsNullOrWhiteSpace(token))
                {
                    var retrieveResult = vimPortType.ContinueRetrievePropertiesEx(
                        serviceContent.propertyCollector, token);
                    if (retrieveResult != null)
                    {
                        token = retrieveResult.token;
                        objectContents.AddRange(retrieveResult.objects);
                    }
                }
            }
            foreach (var content in objectContents)
            {
                if (content.propSet[0].val.ToString() == clusterName)
                {
                    return(content.obj);
                }
            }
            throw new Exception("Could not find cluster with name '" +
                                clusterName + "'");
        }
Ejemplo n.º 4
0
        public override void Run()
        {
            // Login
            VapiAuthHelper           = new VapiAuthenticationHelper();
            VimAuthHelper            = new VimAuthenticationHelper();
            SessionStubConfiguration =
                VapiAuthHelper.LoginByUsernameAndPassword(
                    Server, UserName, Password);
            VimAuthHelper.LoginByUsernameAndPassword(
                Server, UserName, Password);

            // Get the cluster
            ManagedObjectReference clusterMoRef =
                VimHelper.GetCluster(ClusterName, VimAuthHelper);

            this.clusterId = new DynamicID();
            this.clusterId.SetType((string)clusterMoRef.type);
            this.clusterId.SetId((string)clusterMoRef.Value);


            this.tagName = RandomIdGenerator.GetRandomString("Tag-");
            var tagDesc = "Sample tag";

            this.categoryName = RandomIdGenerator.GetRandomString("Cat-");
            var categoryDesc = "Sample category";

            // create services
            this.tagService = VapiAuthHelper.StubFactory.CreateStub <cis.tagging.Tag>(
                SessionStubConfiguration);
            this.categoryService =
                VapiAuthHelper.StubFactory.CreateStub <Category>(
                    SessionStubConfiguration);
            this.tagAssociation =
                VapiAuthHelper.StubFactory.CreateStub <TagAssociation>(
                    SessionStubConfiguration);

            // create a category
            this.categoryId = CreateCategory(categoryService, categoryName,
                                             categoryDesc, CategoryModel.Cardinality.MULTIPLE);
            Console.WriteLine("Created category '{0}'", categoryName);

            // create a tag
            this.tagId = CreateTag(
                this.tagService, this.tagName, tagDesc, this.categoryId);
            Console.WriteLine("Created tag '{0}'", this.tagName);

            // update the category
            var newCategoryDesc = "Tag category updated at " + DateTime.Now;

            UpdateCategoryDesc(this.categoryService, this.categoryId,
                               newCategoryDesc);
            Console.WriteLine("Updated category description to '{0}'",
                              newCategoryDesc);

            // update the tag
            var newTagDesc = "Tag updated at " + DateTime.Now;

            UpdateTagDesc(tagService, tagId, newTagDesc);
            Console.WriteLine("Updated tag description to '{0}'", newTagDesc);

            // tag the Cluster with the newely created tag
            this.tagAssociation.Attach(this.tagId, this.clusterId);
            if (this.tagAssociation.ListAttachedTags(
                    this.clusterId).Contains(this.tagId))
            {
                Console.WriteLine("Cluster '{0}' tagged with '{1}'",
                                  ClusterName, tagName);
                this.tagAttached = true;
            }
            else
            {
                throw new Exception(string.Format(
                                        "Could not tag Cluster '{0}' with '{1}'",
                                        ClusterName, tagName));
            }
        }