public void GetSystemClient_considers_configuration(string configurationName)
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);
            var filesystemMock  = SetupEnvironmentAndFileSystemWithClientKey(environmentMock);

            var lockPath = configurationName == "local" ? "identity" : "zero";

            environmentMock.Setup(x => x.IsOsPlatform(It.Is <OSPlatform>(p => p == OSPlatform.Windows))).Returns(true);
            environmentMock.Setup(x => x.IsWindowsAdminUser).Returns(true);

            filesystemMock.Setup(x =>
                                 x.OpenText(It.Is <string>(p => p.EndsWith($"{lockPath}{Path.DirectorySeparatorChar}.lock"))))
            .Returns(() => new StringReader($"{{\"processName\":\"TestingEryph\",\"processId\":100,\"endpoints\":{{\"identity\":\"http://localhost\"}}}}"));

            environmentMock.Setup(x => x.IsProcessRunning("TestingEryph", 100)).Returns(true);

            var lookup = new ClientCredentialsLookup(environmentMock.Object);

            if (configurationName != "zero" && configurationName != "local")
            {
                Assert.Throws <InvalidOperationException>(() => lookup.GetSystemClientCredentials(configurationName));
            }
            else
            {
                var systemClient = lookup.GetSystemClientCredentials(configurationName);
                Assert.NotNull(systemClient);
            }

            filesystemMock.Verify();
        }
Пример #2
0
        protected ClientCredentials GetClientCredentials()
        {
            var clientCredentials = Credentials;

            if (clientCredentials != null)
            {
                return(clientCredentials);
            }


            var lookup = new ClientCredentialsLookup(new PowershellEnvironment(SessionState));

            clientCredentials = lookup.FindCredentials();

            if (clientCredentials == null)
            {
                throw new InvalidOperationException(@"Could not find credentials for eryph.
You can use the parameter Credentials to set the eryph credentials. If not set, the credentials will be searched in your local configuration. 
If there is no default eryph client in your configuration the command will try to access the default system-client of a local running eryph zero or identity server.
To access the system-client you will have to run this command as Administrator (Windows) or root (Linux).
");
            }

            return(clientCredentials);
        }
        public void GetSystemClient_Throws_if_not_AdminOn_Windows()
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);

            environmentMock.Setup(x => x.IsOsPlatform(It.Is <OSPlatform>(p => p == OSPlatform.Windows))).Returns(true);
            environmentMock.Setup(x => x.IsWindowsAdminUser).Returns(false);

            var lookup = new ClientCredentialsLookup(environmentMock.Object);

            Assert.Throws <InvalidOperationException>(() => lookup.GetSystemClientCredentials());
        }
        public void GetSystemClient_returns_null_if_process_not_running(bool forEryphZero)
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);
            var filesystemMock  = SetupEnvironmentAndFileSystemWithClientKey(environmentMock);

            var moduleName = forEryphZero ? "zero" : "identity";

            filesystemMock.Setup(x =>
                                 x.OpenText(It.Is <string>(p => p.EndsWith($"{moduleName}{Path.DirectorySeparatorChar}.run_info"))))
            .Returns(new StringReader("{\"process_id\" : 100, \"url\" : \"http://eryph.io\"}"));


            environmentMock.Setup(x => x.IsProcessRunning("", 100)).Returns(false);

            var lookup = new ClientCredentialsLookup(environmentMock.Object);

            Assert.Null(lookup.GetSystemClientCredentials());
        }
        public void GetSystemClient_reads_process_info(string baseUrl, string identityEndpoint)
        {
            var environmentMock = new Mock <IEnvironment>(MockBehavior.Strict);
            var filesystemMock  = SetupEnvironmentAndFileSystemWithClientKey(environmentMock);


            filesystemMock.Setup(x =>
                                 x.OpenText(It.Is <string>(p => p.EndsWith(".lock"))))
            .Returns(() => new StringReader($"{{\"processName\":\"TestingEryph\",\"processId\":100,\"endpoints\":{{\"identity\":\"{baseUrl}\"}}}}"));

            environmentMock.Setup(x => x.IsProcessRunning("TestingEryph", 100)).Returns(true);

            var lookup   = new ClientCredentialsLookup(environmentMock.Object);
            var response = lookup.GetSystemClientCredentials();

            Assert.NotNull(response);
            Assert.Equal(identityEndpoint, response.IdentityProvider.ToString());
            Assert.NotNull(response.KeyPairData);
            Assert.Equal("system-client", response.Id);

            environmentMock.Verify();
            filesystemMock.Verify();
        }
        public static Task <ServiceClientCredentials> LogonWithEryphClient(IEnumerable <string> scopes)
        {
            var clientLookup = new ClientCredentialsLookup();

            return(LogonWithEryphClient(clientLookup.FindCredentials(), scopes));
        }