use of com.yahoo.athenz.zms.PublicKeyEntry in project athenz by yahoo.
the class JDBCConnection method getAthenzDomainServices.
void getAthenzDomainServices(String domainName, int domainId, AthenzDomain athenzDomain, String caller) {
Map<String, ServiceIdentity> serviceMap = new HashMap<>();
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String serviceName = rs.getString(ZMSConsts.DB_COLUMN_NAME);
ServiceIdentity service = new ServiceIdentity().setName(ZMSUtils.serviceResourceName(domainName, serviceName)).setProviderEndpoint(saveValue(rs.getString(ZMSConsts.DB_COLUMN_PROVIDER_ENDPOINT))).setExecutable(saveValue(rs.getString(ZMSConsts.DB_COLUMN_EXECTUABLE))).setUser(saveValue(rs.getString(ZMSConsts.DB_COLUMN_SVC_USER))).setGroup(saveValue(rs.getString(ZMSConsts.DB_COLUMN_SVC_GROUP))).setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime()));
List<PublicKeyEntry> publicKeys = new ArrayList<>();
service.setPublicKeys(publicKeys);
serviceMap.put(serviceName, service);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES_HOSTS)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String serviceName = rs.getString(1);
ServiceIdentity service = serviceMap.get(serviceName);
if (service == null) {
continue;
}
List<String> hosts = service.getHosts();
if (hosts == null) {
hosts = new ArrayList<>();
service.setHosts(hosts);
}
hosts.add(rs.getString(2));
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES_PUBLIC_KEYS)) {
ps.setInt(1, domainId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
String serviceName = rs.getString(1);
ServiceIdentity service = serviceMap.get(serviceName);
if (service == null) {
continue;
}
PublicKeyEntry publicKey = new PublicKeyEntry().setId(rs.getString(ZMSConsts.DB_COLUMN_KEY_ID)).setKey(rs.getString(ZMSConsts.DB_COLUMN_KEY_VALUE));
service.getPublicKeys().add(publicKey);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
athenzDomain.getServices().addAll(serviceMap.values());
}
use of com.yahoo.athenz.zms.PublicKeyEntry in project athenz by yahoo.
the class SignUtils method asStruct.
private static Struct asStruct(ServiceIdentity service) {
// all of our fields are in canonical order based
// on their attribute name
Struct struct = new Struct();
appendObject(struct, ATTR_DESCRIPTION, service.getDescription());
appendObject(struct, ATTR_EXECUTABLE, service.getExecutable());
appendObject(struct, ATTR_GROUP, service.getGroup());
appendList(struct, ATTR_HOSTS, service.getHosts());
appendObject(struct, ATTR_MODIFIED, service.getModified());
appendObject(struct, ATTR_NAME, service.getName());
appendObject(struct, ATTR_PROVIDER_ENDPOINT, service.getProviderEndpoint());
List<PublicKeyEntry> publicKeys = service.getPublicKeys();
Array publicKeysArray = new Array();
if (publicKeys != null) {
for (PublicKeyEntry publicKey : publicKeys) {
Struct structPublicKey = new Struct();
appendObject(structPublicKey, ATTR_ID, publicKey.getId());
appendObject(structPublicKey, ATTR_KEY, publicKey.getKey());
publicKeysArray.add(structPublicKey);
}
}
appendArray(struct, ATTR_PUBLIC_KEYS, publicKeysArray);
appendObject(struct, ATTR_USER, service.getUser());
return struct;
}
use of com.yahoo.athenz.zms.PublicKeyEntry in project athenz by yahoo.
the class JDBCConnectionTest method testUpdatePublicKeyEntry.
@Test
public void testUpdatePublicKeyEntry() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
PublicKeyEntry publicKey = new PublicKeyEntry().setId("zms1").setKey("Value1");
Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
5).thenReturn(// service id
7);
Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
true).thenReturn(// this one is for service id
true);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
boolean requestSuccess = jdbcConn.updatePublicKeyEntry("my-domain", "service1", publicKey);
assertTrue(requestSuccess);
// getting domain and service ids
Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
Mockito.verify(mockPrepStmt, times(1)).setString(2, "service1");
// public key entry statement
Mockito.verify(mockPrepStmt, times(1)).setString(1, "Value1");
Mockito.verify(mockPrepStmt, times(1)).setInt(2, 7);
Mockito.verify(mockPrepStmt, times(1)).setString(3, "zms1");
jdbcConn.close();
}
use of com.yahoo.athenz.zms.PublicKeyEntry in project athenz by yahoo.
the class JDBCConnectionTest method testUpdatePublicKeyEntryInvalidDomain.
@Test
public void testUpdatePublicKeyEntryInvalidDomain() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
PublicKeyEntry publicKey = new PublicKeyEntry().setId("zms1").setKey("Value1");
Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
false);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
try {
jdbcConn.updatePublicKeyEntry("my-domain", "service1", publicKey);
fail();
} catch (Exception ex) {
assertTrue(true);
}
jdbcConn.close();
}
use of com.yahoo.athenz.zms.PublicKeyEntry in project athenz by yahoo.
the class JDBCConnectionTest method testInsertPublicKeyEntryInvalidDomain.
@Test
public void testInsertPublicKeyEntryInvalidDomain() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
PublicKeyEntry publicKey = new PublicKeyEntry().setId("zms1").setKey("Value1");
Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
false);
Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
try {
jdbcConn.insertPublicKeyEntry("my-domain", "service1", publicKey);
fail();
} catch (Exception ex) {
assertTrue(true);
}
jdbcConn.close();
}
Aggregations