@NotNull public static Map<String, String> toProperties(@NotNull ServerConfig config, @Nullable String valueIfUnset, @Nullable String valueIfPasswordStored, @Nullable String valueIfPasswordNotStored) { Map<String, String> ret = new HashMap<>(); ret.put(PerforceEnvironment.P4PORT, config.getServerName().getDisplayName()); ret.put(PerforceEnvironment.P4TRUST, ! config.hasTrustTicket() || config.getTrustTicket() == null ? valueIfUnset : config.getTrustTicket().toString()); ret.put(PerforceEnvironment.P4USER, config.getUsername()); ret.put(PerforceEnvironment.P4TICKETS, ! config.hasAuthTicket() || config.getAuthTicket() == null ? valueIfUnset : config.getAuthTicket().toString()); ret.put(PerforceEnvironment.P4SERVER_FINGERPRINT, ! config.hasServerFingerprint() || config.getServerFingerprint() == null ? valueIfUnset : config.getServerFingerprint()); ret.put(PerforceEnvironment.P4PASSWD, config.usesStoredPassword() ? valueIfPasswordStored : valueIfPasswordNotStored); ret.put(PerforceEnvironment.P4LOGINSSO, ! config.hasLoginSso() || config.getLoginSso() == null ? valueIfUnset : config.getLoginSso()); return ret; }
@Nullable public ClientConfig loadConfigFromSettings() { ConfigPart parentPart = loadParentPartFromSettings(); if (!parentPart.hasError() && ServerConfig.isValidServerConfig(parentPart)) { try { ServerConfig serverConfig = ServerConfig.createFrom(parentPart); if (ClientConfig.isValidClientConfig(serverConfig, parentPart)) { return ClientConfig.createFrom(serverConfig, parentPart); } } catch (IllegalArgumentException e) { LOG.info("Should have not caused an error due to previous error check", e); } } return null; }
@Override protected void onServerConnected(@NotNull ServerConnectedMessage.ServerConnectedEvent e) { // Note: does not check disposed state. getServersFor(e.getServerConfig().getServerName()).forEach((state) -> { state.setServerHostProblem(false); // If the connectivity and login all lines up, then the login works. if (e.isLoggedIn() && e.getServerConfig().getServerId().equals(state.getServerConfig().getServerId())) { state.setServerLoginProblem(false); } }); }
@NotNull private Properties createProperties(@NotNull ServerConfig serverConfig) { final Properties props = createProperties(); // Should always be set. props.setProperty(PropertyDefs.USER_NAME_KEY, serverConfig.getUsername()); if (serverConfig.hasAuthTicket() && serverConfig.getAuthTicket() != null) { props.setProperty(PropertyDefs.TICKET_PATH_KEY, serverConfig.getAuthTicket().getAbsolutePath()); } // This doesn't actually do anything with the default connection that we use. // props.setProperty(PropertyDefs.PASSWORD_KEY, knownPassword); if (serverConfig.hasTrustTicket() && serverConfig.getTrustTicket() != null) { props.setProperty(PropertyDefs.TRUST_PATH_KEY, serverConfig.getTrustTicket().getAbsolutePath()); } if (serverConfig.hasTrustTicket() && serverConfig.getTrustTicket() != null) { props.setProperty(PropertyDefs.TRUST_PATH_KEY, serverConfig.getTrustTicket().getAbsolutePath()); } return props; }
@NotNull protected CredentialAttributes getCredentialAttributes(@NotNull ServerConfig config, boolean inMemory) { return new CredentialAttributes( "p4ic4idea:" + config.getServerName().getFullPort(), config.getUsername(), null, inMemory); } }
@Test void createFrom_empty() { ServerConfig sc = ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") ); assertEquals(P4ServerName.forPort("servername"), sc.getServerName()); assertEquals("username", sc.getUsername()); assertNull(sc.getServerFingerprint()); assertNull(sc.getAuthTicket()); assertNull(sc.getTrustTicket()); assertNull(sc.getLoginSso()); assertThat(sc.getConfigVersion(), greaterThan(0)); assertFalse(sc.hasServerFingerprint()); assertFalse(sc.hasAuthTicket()); assertFalse(sc.hasTrustTicket()); assertFalse(sc.hasLoginSso()); assertFalse(sc.usesStoredPassword()); assertEquals("localhost:servername" + SEP + "username" + SEP + "null" + SEP + "null" + SEP + "null", sc.getServerId()); }
if (! isEqual(getServerName(), part.getServerName())) { if (LOG.isDebugEnabled()) { if (part.getServerName() == null) { } else { LOG.debug("isSameServerConnection: server doesn't match: " + getServerName().getServerPort() + "::" + getServerName().getServerProtocol() + " <> " + part.getServerName().getServerPort() + "::" + part.getServerName().getServerProtocol()); if (! filesEqual(hasAuthTicket(), getAuthTicket(), part.hasAuthTicketFileSet(), part.getAuthTicketFile())) { if (LOG.isDebugEnabled()) { LOG.debug("isSameServerConnection: auth ticket doesn't match: " + getAuthTicket() + " <> " + part.getAuthTicketFile()); if (! filesEqual(hasTrustTicket(), getTrustTicket(), part.hasTrustTicketFileSet(), part.getTrustTicketFile())) { if (LOG.isDebugEnabled()) { LOG.debug("isSameServerConnection: trust ticket doesn't match: " + getTrustTicket() + " <> " + part.getTrustTicketFile()); if (hasServerFingerprint() != part.hasServerFingerprintSet()) { if (LOG.isDebugEnabled()) { LOG.debug("isSameServerConnection: has server fingerprint mismatch: " + hasServerFingerprint() + " <> " + part.hasServerFingerprintSet()); if (hasServerFingerprint() && ! isEqual(getServerFingerprint(), part.getServerFingerprint())) { if (LOG.isDebugEnabled()) { LOG.debug("isSameServerConnection: server fingerprint mismatch: " + getServerFingerprint() + " <> " + part.getServerFingerprint());
ConnectionInfo(@NotNull ServerConfig config) { this.clientConfig = null; this.serverConfig = config; this.serverName = config.getServerName(); }
@Test void getCachedOpenedChangelists_noActions() { MockConfigPart configPart = createConfigPart(); ServerConfig serverConfig = ServerConfig.createFrom(configPart); ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, configPart); ProjectCacheStore projectStore = new ProjectCacheStore(); CacheQueryHandlerImpl query = new CacheQueryHandlerImpl(idea.getMockProject(), projectStore); ClientQueryCacheStore clientStore = new ClientQueryCacheStore(clientConfig.getClientServerRef()); P4LocalChangelist cl1 = new P4LocalChangelistImpl.Builder() .withChangelistId(new P4ChangelistIdImpl(1, clientConfig.getClientServerRef())) .withComment("comment 1") .withClientname(clientConfig.getClientname()) .withUsername(serverConfig.getUsername()) .build(); clientStore.setChangelists(cl1); projectStore.addCache(clientStore); Collection<P4LocalChangelist> changes = query.getCachedOpenedChangelists(clientConfig); assertSize(1, changes); assertEqualChangelists(cl1, changes.iterator().next()); }
@Test void createFrom_error() { assertThrows(IllegalArgumentException.class, () -> ServerConfig.createFrom(new MockConfigPart())); }
public static boolean isValidClientConfig(@Nullable ServerConfig serverConfig, @Nullable ConfigPart part) { if (serverConfig == null || part == null || part.hasError()) { return false; } if (isBlank(part.getClientname())) { return false; } if (isBlank(serverConfig.getUsername())) { return false; } return true; }
.withClientname("lclient") ServerConfig sc = ServerConfig.createFrom(data); ClientConfig cc = ClientConfig.createFrom(sc, data); assertEquals(new ClientServerRef(sc.getServerName(), "lclient"), cc.getClientServerRef()); assertThat(cc.getConfigVersion(), greaterThan(0)); assertEquals( sc.getServerId() + SEP + "lclient" + SEP + null + SEP +
@Override public boolean equals(Object other) { if (other == null) { return false; } if (this == other) { return true; } if (!(other instanceof ServerConfig)) { return false; } ServerConfig sc = (ServerConfig) other; return sc.getServerId().equals(getServerId()); }
public boolean hasLoginSso() { return getLoginSso() != null; }
private ServerConfig(@NotNull ConfigPart part) { if (! isValidServerConfig(part)) { throw new IllegalArgumentException("Did not check validity before creating"); this.usesPassword = part.requiresUserEnteredPassword() || part.hasPasswordSet(); this.serverId = getServerIdForDataPart(part);
@Test void isSameServer_empty() { MockConfigPart part1 = new MockConfigPart() .withUsername("u1") .withServerName("n1:1"); ServerConfig sc = ServerConfig.createFrom(part1); assertTrue(sc.isSameServerConnection(part1)); // Change it in the client ways, and it's still the same server. part1.withClientHostname("h1").withClientname("cc") .withDefaultCharset("a1").withIgnoreFileName("i") .withReloadValue(false); assertTrue(sc.isSameServerConnection(part1)); // Change the user MockConfigPart part2 = part1.copy() .withUsername("u2"); assertFalse(sc.isSameServerConnection(part2)); // change the server name MockConfigPart part3 = part1.copy() .withServerName("n2:1"); assertFalse(sc.isSameServerConnection(part3)); // change the password MockConfigPart part4 = part1.copy() .withPassword("a"); assertFalse(sc.isSameServerConnection(part4)); part4.withNoPassword().withRequiresUserEnteredPassword(true); assertFalse(sc.isSameServerConnection(part4)); }
private Answer<OneTimeString> getPassword(@NotNull final ServerConfig serverConfig) { if (serverConfig.usesStoredPassword()) { if (LOG.isDebugEnabled()) { LOG.debug("Using password stored in registry."); } // Custom promise handler. We want all password responses, whether a failure or a success, // to resolve. Failures shouldn't be bubbled up from this call. return Answer.resolve(null) .futureMap((x, sink) -> ApplicationPasswordRegistry.getInstance().get(serverConfig) .processed(sink::resolve) .rejected((t) -> { LOG.warn("Problem loading the password", t); sink.resolve(new OneTimeString(EMPTY_PASSWORD)); })); } return Answer.resolve(new OneTimeString(EMPTY_PASSWORD)); }
public boolean hasTrustTicket() { return getTrustTicket() != null; }
@Test void isValid() { assertFalse(ServerConfig.isValidServerConfig(null)); MockConfigPart part = new MockConfigPart(); part.withConfigProblems(createError()); assertFalse(ServerConfig.isValidServerConfig(part)); part.withConfigProblems(); assertFalse(ServerConfig.isValidServerConfig(part)); P4ServerName mockName = mock(P4ServerName.class); when(mockName.getFullPort()).thenReturn(""); part.withP4ServerName(mockName); assertFalse(ServerConfig.isValidServerConfig(part)); part.withConfigProblems(createWarning()) .withServerName("my-server"); assertFalse(ServerConfig.isValidServerConfig(part)); part.withUsername(""); assertFalse(ServerConfig.isValidServerConfig(part)); part.withUsername("abc"); assertTrue(ServerConfig.isValidServerConfig(part)); }
public boolean hasAuthTicket() { return getAuthTicket() != null; }