Example #1
0
        public static Mock <IOctopusRepository> AddOctopusRepo(PSVariableIntrinsics psVariable)
        {
            var octoRepo = new Mock <IOctopusRepository>();

            // Add the mock repository to the session to simulate 'Connect-OctoServer'
            psVariable.Set("OctopusRepository", octoRepo.Object);

            return(octoRepo);
        }
Example #2
0
        internal virtual void PrepareSession()
        {
            // make sure we have a valid WebRequestSession object to work with
            if (null == WebSession)
            {
                WebSession = new WebRequestSession();
            }

            if (null != SessionVariable)
            {
                // save the session back to the PS environment if requested
                PSVariableIntrinsics vi = SessionState.PSVariable;
                vi.Set(SessionVariable, WebSession);
            }

            //
            // handle credentials
            //
            if (null != Credential && Authentication == WebAuthenticationType.None)
            {
                // get the relevant NetworkCredential
                NetworkCredential netCred = Credential.GetNetworkCredential();
                WebSession.Credentials = netCred;

                // supplying a credential overrides the UseDefaultCredentials setting
                WebSession.UseDefaultCredentials = false;
            }
            else if ((null != Credential || null != Token) && Authentication != WebAuthenticationType.None)
            {
                ProcessAuthentication();
            }
            else if (UseDefaultCredentials)
            {
                WebSession.UseDefaultCredentials = true;
            }


            if (null != CertificateThumbprint)
            {
                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection collection   = (X509Certificate2Collection)store.Certificates;
                X509Certificate2Collection tbCollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, CertificateThumbprint, false);
                if (tbCollection.Count == 0)
                {
                    CryptographicException ex = new CryptographicException(WebCmdletStrings.ThumbprintNotFound);
                    throw ex;
                }
                foreach (X509Certificate2 tbCert in tbCollection)
                {
                    X509Certificate certificate = (X509Certificate)tbCert;
                    WebSession.AddCertificate(certificate);
                }
            }

            if (null != Certificate)
            {
                WebSession.AddCertificate(Certificate);
            }

            //
            // handle the user agent
            //
            if (null != UserAgent)
            {
                // store the UserAgent string
                WebSession.UserAgent = UserAgent;
            }

            if (null != Proxy)
            {
                WebProxy webProxy = new WebProxy(Proxy);
                webProxy.BypassProxyOnLocal = false;
                if (null != ProxyCredential)
                {
                    webProxy.Credentials = ProxyCredential.GetNetworkCredential();
                }
                else if (ProxyUseDefaultCredentials)
                {
                    // If both ProxyCredential and ProxyUseDefaultCredentials are passed,
                    // UseDefaultCredentials will overwrite the supplied credentials.
                    webProxy.UseDefaultCredentials = true;
                }
                WebSession.Proxy = webProxy;
            }

            if (-1 < MaximumRedirection)
            {
                WebSession.MaximumRedirection = MaximumRedirection;
            }

            // store the other supplied headers
            if (null != Headers)
            {
                foreach (string key in Headers.Keys)
                {
                    // add the header value (or overwrite it if already present)
                    WebSession.Headers[key] = Headers[key].ToString();
                }
            }
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of <see cref="SessionVariables"/>.
 /// </summary>
 /// <param name="variables">The inner variables object</param>
 public SessionVariables(PSVariableIntrinsics variables)
 {
     _variables = variables;
 }
        /// <summary>
        /// Process the web response and output corresponding objects.
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            if (null == response)
            {
                throw new ArgumentNullException("response");
            }

            using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
            {
                if (ShouldWriteToPipeline)
                {
                    // First see if it is an RSS / ATOM feed, in which case we can
                    // stream it - unless the user has overridden it with a return type of "XML"
                    if (TryProcessFeedStream(responseStream))
                    {
                        // Do nothing, content has been processed.
                    }
                    else
                    {
                        // determine the response type
                        RestReturnType returnType = CheckReturnType(response);

                        // Try to get the response encoding from the ContentType header.
                        Encoding encoding = null;
                        string   charSet  = response.Content.Headers.ContentType?.CharSet;
                        if (!string.IsNullOrEmpty(charSet))
                        {
                            // NOTE: Don't use ContentHelper.GetEncoding; it returns a
                            // default which bypasses checking for a meta charset value.
                            StreamHelper.TryGetEncoding(charSet, out encoding);
                        }

                        object    obj = null;
                        Exception ex  = null;

                        string str = StreamHelper.DecodeStream(responseStream, ref encoding);
                        // NOTE: Tests use this verbose output to verify the encoding.
                        WriteVerbose(string.Format
                                     (
                                         System.Globalization.CultureInfo.InvariantCulture,
                                         "Content encoding: {0}",
                                         string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName)
                                     );
                        bool convertSuccess = false;

                        if (returnType == RestReturnType.Json)
                        {
                            convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
                        }
                        // default to try xml first since it's more common
                        else
                        {
                            convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
                        }

                        if (!convertSuccess)
                        {
                            // fallback to string
                            obj = str;
                        }

                        WriteObject(obj);
                    }
                }

                if (ShouldSaveToOutFile)
                {
                    StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
                }

                if (!String.IsNullOrEmpty(ResponseHeadersVariable))
                {
                    PSVariableIntrinsics vi = SessionState.PSVariable;
                    vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response));
                }
            }
        }
Example #5
0
        /// <summary>
        /// Sets the <see cref="RestartManagerSession"/> session variable.
        /// </summary>
        /// <param name="services">Optional services to use to resolve variables.</param>
        /// <param name="variables">Variables for the current <see cref="Runspace"/>.</param>
        /// <param name="session">The <see cref="RestartManagerSession"/> to set.</param>
        public static void SetVariable(IServiceProvider services, PSVariableIntrinsics variables, RestartManagerSession session)
        {
            var resolver = services?.GetService <IVariableService>() ?? new RunspaceVariableService(variables);

            resolver.SetValue(VariableName, session);
        }
Example #6
0
        /// <summary>
        /// Gets a <see cref="RestartManagerSession"/> from a parameter or from a variable.
        /// </summary>
        /// <param name="services">Optional services to use to resolve variables.</param>
        /// <param name="session">A backing field for a parameter that may reference a <see cref="RestartManagerSession"/>.</param>
        /// <param name="variables">Variables for the current <see cref="Runspace"/>.</param>
        /// <returns>A <see cref="RestartManagerSession"/> from a parameter or from a variable.</returns>
        /// <exception cref="NoSessionException">No <see cref="RestartManagerSession"/> is available.</exception>
        public static RestartManagerSession GetParameterOrVariable(IServiceProvider services, ref RestartManagerSession session, PSVariableIntrinsics variables)
        {
            if (session == null)
            {
                var resolver = services?.GetService <IVariableService>() ?? new RunspaceVariableService(variables);
                session = resolver.GetValue <RestartManagerSession>(VariableName);
            }

            return(session ?? throw new NoSessionException());
        }
Example #7
0
        /// <summary>
        /// Gets a <see cref="RestartManagerSession"/> from a variable.
        /// </summary>
        /// <param name="services">Optional services to use to resolve variables.</param>
        /// <param name="variables">Variables for the current <see cref="Runspace"/>.</param>
        /// <returns>A <see cref="RestartManagerSession"/> from a variable or null if no session is available.</returns>
        public static RestartManagerSession GetVariable(IServiceProvider services, PSVariableIntrinsics variables)
        {
            var resolver = services?.GetService <IVariableService>() ?? new RunspaceVariableService(variables);

            return(resolver.GetValue <RestartManagerSession>(VariableName));
        }
Example #8
0
 // Token: 0x060004FF RID: 1279 RVA: 0x00011C3F File Offset: 0x0000FE3F
 public PSVariables(PSVariableIntrinsics psVariables)
 {
     this.variables = psVariables;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RunspaceVariableService"/> class.
 /// </summary>
 /// <param name="variables">The <see cref="PSVariableIntrinsics"/> to wrap.</param>
 public RunspaceVariableService(PSVariableIntrinsics variables)
 {
     this.variables = variables;
 }