Skip to content

Commit ae12a13

Browse files
mackdkmcalmer
authored andcommitted
Add new credentials type for the hub scc endpoint
1 parent 7cb4ffc commit ae12a13

File tree

11 files changed

+210
-35
lines changed

11 files changed

+210
-35
lines changed

java/code/src/com/redhat/rhn/common/hibernate/AnnotationRegistry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018--2024 SUSE LLC
2+
* Copyright (c) 2018--2025 SUSE LLC
33
*
44
* This software is licensed to you under the GNU General Public License,
55
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
@@ -31,6 +31,7 @@
3131
import com.redhat.rhn.domain.contentmgmt.SoftwareProjectSource;
3232
import com.redhat.rhn.domain.credentials.BaseCredentials;
3333
import com.redhat.rhn.domain.credentials.CloudRMTCredentials;
34+
import com.redhat.rhn.domain.credentials.HubSCCCredentials;
3435
import com.redhat.rhn.domain.credentials.RHUICredentials;
3536
import com.redhat.rhn.domain.credentials.RegistryCredentials;
3637
import com.redhat.rhn.domain.credentials.ReportDBCredentials;
@@ -194,6 +195,7 @@ private AnnotationRegistry() {
194195
RHUICredentials.class,
195196
SCCCredentials.class,
196197
VHMCredentials.class,
198+
HubSCCCredentials.class,
197199
ChannelSyncFlag.class,
198200
ServerCoCoAttestationConfig.class,
199201
ServerCoCoAttestationReport.class,

java/code/src/com/redhat/rhn/domain/credentials/CredentialsFactory.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
/*
2-
* Copyright (c) 2012 SUSE LLC
2+
* Copyright (c) 2012--2025 SUSE LLC
33
*
44
* This software is licensed to you under the GNU General Public License,
55
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
66
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
77
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
88
* along with this software; if not, see
99
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
10-
*
11-
* Red Hat trademarks are not licensed under GPLv2. No permission is
12-
* granted to use or replicate Red Hat trademarks that are incorporated
13-
* in this software or its documentation.
1410
*/
1511

1612
package com.redhat.rhn.domain.credentials;
@@ -92,6 +88,17 @@ public static SCCCredentials createSCCCredentials(String username, String passwo
9288
return new SCCCredentials(username, password);
9389
}
9490

91+
/**
92+
* Helper method for creating new Hub SCC {@link Credentials}
93+
* @param username the username
94+
* @param password the password that will be BASE64 encoded
95+
* @param fqdn the FQDN of the peripheral server that will use this credentials
96+
* @return new credential with type SCC
97+
*/
98+
public static HubSCCCredentials createHubSCCCredentials(String username, String password, String fqdn) {
99+
return new HubSCCCredentials(username, password, fqdn);
100+
}
101+
95102
/**
96103
* Helper method for creating new Virtual Host Manager {@link Credentials}
97104
* @param username the username

java/code/src/com/redhat/rhn/domain/credentials/CredentialsType.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
/*
2-
* Copyright (c) 2012 SUSE LLC
2+
* Copyright (c) 2012--2025 SUSE LLC
33
*
44
* This software is licensed to you under the GNU General Public License,
55
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
66
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
77
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
88
* along with this software; if not, see
99
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
10-
*
11-
* Red Hat trademarks are not licensed under GPLv2. No permission is
12-
* granted to use or replicate Red Hat trademarks that are incorporated
13-
* in this software or its documentation.
1410
*/
1511
package com.redhat.rhn.domain.credentials;
1612

@@ -25,7 +21,8 @@ public enum CredentialsType {
2521
REGISTRY(Label.REGISTRY),
2622
CLOUD_RMT(Label.CLOUD_RMT),
2723
REPORT_DATABASE(Label.REPORT_DATABASE),
28-
RHUI(Label.RHUI);
24+
RHUI(Label.RHUI),
25+
HUB_SCC(Label.HUB_SCC);
2926

3027
private final String label;
3128

@@ -58,6 +55,7 @@ public static class Label {
5855
public static final String CLOUD_RMT = "cloudrmt";
5956
public static final String REPORT_DATABASE = "reportcreds";
6057
public static final String RHUI = "rhui";
58+
public static final String HUB_SCC = "hub_scc";
6159

6260
private Label() {
6361
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2025 SUSE LLC
3+
*
4+
* This software is licensed to you under the GNU General Public License,
5+
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
6+
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
7+
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
8+
* along with this software; if not, see
9+
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
10+
*/
11+
12+
package com.redhat.rhn.domain.credentials;
13+
14+
import org.apache.commons.lang3.builder.EqualsBuilder;
15+
import org.apache.commons.lang3.builder.HashCodeBuilder;
16+
import org.apache.commons.lang3.builder.ToStringBuilder;
17+
18+
import javax.persistence.Column;
19+
import javax.persistence.DiscriminatorValue;
20+
import javax.persistence.Entity;
21+
import javax.persistence.Transient;
22+
23+
@Entity
24+
@DiscriminatorValue(CredentialsType.Label.HUB_SCC)
25+
public class HubSCCCredentials extends PasswordBasedCredentials {
26+
27+
private String peripheralUrl;
28+
29+
// No args constructor for hibernate
30+
protected HubSCCCredentials() {
31+
}
32+
33+
// Default constructor filling the mandatory fields to be used in the CredentialFactory
34+
protected HubSCCCredentials(String usernameIn, String passwordIn, String peripheralUrlIn) {
35+
setUsername(usernameIn);
36+
setPassword(passwordIn);
37+
this.peripheralUrl = peripheralUrlIn;
38+
}
39+
40+
@Override
41+
@Transient
42+
public CredentialsType getType() {
43+
return CredentialsType.HUB_SCC;
44+
}
45+
46+
@Column(name = "url")
47+
public String getPeripheralUrl() {
48+
return peripheralUrl;
49+
}
50+
51+
public void setPeripheralUrl(String peripheralUrlIn) {
52+
this.peripheralUrl = peripheralUrlIn;
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) {
58+
return true;
59+
}
60+
61+
if (!(o instanceof HubSCCCredentials that)) {
62+
return false;
63+
}
64+
65+
return new EqualsBuilder()
66+
.appendSuper(super.equals(o))
67+
.append(getPeripheralUrl(), that.getPeripheralUrl())
68+
.isEquals();
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return new HashCodeBuilder(17, 37)
74+
.appendSuper(super.hashCode())
75+
.append(getPeripheralUrl())
76+
.toHashCode();
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return new ToStringBuilder(this)
82+
.append("id", getId())
83+
.append("type", CredentialsType.HUB_SCC)
84+
.append("user", getUser())
85+
.append("username", getUsername())
86+
.append("url", getPeripheralUrl())
87+
.toString();
88+
}
89+
}

java/code/src/com/suse/manager/iss/SyncController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.suse.manager.webui.utils.SparkApplicationHelper.success;
2222
import static spark.Spark.post;
2323

24+
import com.redhat.rhn.domain.credentials.HubSCCCredentials;
2425
import com.redhat.rhn.domain.iss.IssRole;
2526

2627
import com.suse.manager.model.hub.HubManager;
@@ -112,8 +113,8 @@ private String generateCredentials(Request request, Response response, Token tok
112113
return badRequest(response, "Specified FQDN is not a known peripheral");
113114
}
114115

115-
SCCCredentialsJson credentialsJson = hubManager.generateSCCCredentials(peripheral.getId());
116-
return success(response, credentialsJson);
116+
HubSCCCredentials credentials = hubManager.generateSCCCredentials(peripheral);
117+
return success(response, new SCCCredentialsJson(credentials.getUsername(), credentials.getPassword()));
117118
}
118119

119120
private String storeCredentials(Request request, Response response, Token token, String fqdn) {
@@ -126,7 +127,6 @@ private String storeCredentials(Request request, Response response, Token token,
126127
}
127128

128129
hubManager.storeSCCCredentials(hub, storeRequest.getUsername(), storeRequest.getPassword());
129-
130130
return success(response);
131131
}
132132
}

java/code/src/com/suse/manager/model/hub/HubManager.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import com.redhat.rhn.common.conf.ConfigDefaults;
1515
import com.redhat.rhn.domain.credentials.CredentialsFactory;
16+
import com.redhat.rhn.domain.credentials.HubSCCCredentials;
1617
import com.redhat.rhn.domain.credentials.SCCCredentials;
1718
import com.redhat.rhn.domain.iss.IssRole;
1819
import com.redhat.rhn.domain.role.RoleFactory;
@@ -210,28 +211,30 @@ public void register(String remoteServer, IssRole role, String remoteToken, Stri
210211

211212
/**
212213
* Generate SCC credentials for the specified peripheral
213-
* @param peripheralId the id of the peripheral server
214-
* @return the generated credentials
214+
* @param peripheral the id of the peripheral server
215+
* @return the generated {@link HubSCCCredentials}
215216
*/
216-
public SCCCredentialsJson generateSCCCredentials(long peripheralId) {
217-
String username = "peripheral-%06d".formatted(peripheralId);
217+
public HubSCCCredentials generateSCCCredentials(IssPeripheral peripheral) {
218+
String username = "peripheral-%06d".formatted(peripheral.getId());
218219
String password = RandomStringUtils.random(24, 0, 0, true, true, null, new SecureRandom());
219220

220-
/*
221-
* TODO Store and return the credentials. This involves creating a new credential type not yet defined.
222-
* For now, just return a SCCCredentialsJson object as temporary wrapper of username/password
223-
*/
221+
var hubSCCCredentials = CredentialsFactory.createHubSCCCredentials(username, password, peripheral.getFqdn());
222+
CredentialsFactory.storeCredentials(hubSCCCredentials);
224223

225-
return new SCCCredentialsJson(username, password);
224+
peripheral.setMirrorCredentials(hubSCCCredentials);
225+
saveServer(peripheral);
226+
227+
return hubSCCCredentials;
226228
}
227229

228230
/**
229231
* Store the given SCC credentials into the credentials database
230232
* @param hub the FQDN of the hub of this credentials
231233
* @param username the username
232234
* @param password the password
235+
* @return the stored {@link SCCCredentials}
233236
*/
234-
public void storeSCCCredentials(IssHub hub, String username, String password) {
237+
public SCCCredentials storeSCCCredentials(IssHub hub, String username, String password) {
235238
// Delete any existing SCC Credentials
236239
CredentialsFactory.listSCCCredentials()
237240
.forEach(creds -> mirrorCredentialsManager.deleteMirrorCredentials(creds.getId(), null));
@@ -245,6 +248,8 @@ public void storeSCCCredentials(IssHub hub, String username, String password) {
245248

246249
hub.setMirrorCredentials(credentials);
247250
saveServer(hub);
251+
252+
return credentials;
248253
}
249254

250255
private void registerWithToken(String remoteServer, IssRole role, String rootCA, String remoteToken)
@@ -270,9 +275,9 @@ private void registerToRemote(IssServer remoteServer, String remoteToken, String
270275

271276
internalApi.register(localRoleForRemote, localAccessToken, localRootCA);
272277

273-
if (remoteServer instanceof IssPeripheral) {
278+
if (remoteServer instanceof IssPeripheral peripheral) {
274279
// if the remote server is a peripheral, generate the scc credentials for it
275-
SCCCredentialsJson credentials = generateSCCCredentials(remoteServer.getId());
280+
HubSCCCredentials credentials = generateSCCCredentials(peripheral);
276281
internalApi.storeCredentials(credentials.getUsername(), credentials.getPassword());
277282
}
278283
else if (remoteServer instanceof IssHub hub) {

java/code/src/com/suse/manager/model/hub/IssPeripheral.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
package com.suse.manager.model.hub;
1212

1313
import com.redhat.rhn.domain.BaseDomainHelper;
14-
import com.redhat.rhn.domain.credentials.SCCCredentials;
14+
import com.redhat.rhn.domain.credentials.HubSCCCredentials;
1515
import com.redhat.rhn.domain.iss.IssRole;
1616

1717
import org.apache.commons.lang3.builder.HashCodeBuilder;
@@ -38,7 +38,7 @@ public class IssPeripheral extends BaseDomainHelper implements IssServer {
3838
private Long id;
3939
private String fqdn;
4040
private String rootCa;
41-
private SCCCredentials mirrorCredentials;
41+
private HubSCCCredentials mirrorCredentials;
4242
private Set<IssPeripheralChannels> peripheralChannels;
4343

4444
protected IssPeripheral() {
@@ -106,9 +106,9 @@ public String getRootCa() {
106106
* Get the mirror credentials.
107107
* @return the credentials
108108
*/
109-
@ManyToOne(targetEntity = SCCCredentials.class)
109+
@ManyToOne(targetEntity = HubSCCCredentials.class)
110110
@JoinColumn(name = "mirror_creds_id")
111-
public SCCCredentials getMirrorCredentials() {
111+
public HubSCCCredentials getMirrorCredentials() {
112112
return mirrorCredentials;
113113
}
114114

@@ -146,7 +146,7 @@ public void setRootCa(String rootCaIn) {
146146
/**
147147
* @param mirrorCredentialsIn the mirror credentials
148148
*/
149-
public void setMirrorCredentials(SCCCredentials mirrorCredentialsIn) {
149+
public void setMirrorCredentials(HubSCCCredentials mirrorCredentialsIn) {
150150
mirrorCredentials = mirrorCredentialsIn;
151151
}
152152

java/code/src/com/suse/manager/model/hub/test/HubFactoryTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.redhat.rhn.domain.channel.ChannelFactory;
2222
import com.redhat.rhn.domain.channel.test.ChannelFactoryTest;
2323
import com.redhat.rhn.domain.credentials.CredentialsFactory;
24+
import com.redhat.rhn.domain.credentials.HubSCCCredentials;
2425
import com.redhat.rhn.domain.credentials.SCCCredentials;
2526
import com.redhat.rhn.testing.BaseTestCaseWithUser;
2627
import com.redhat.rhn.testing.TestUtils;
@@ -96,7 +97,7 @@ public void testCreateIssPeripheral() {
9697
assertNotNull(issPeripheral.get().getCreated(), "created should not be NULL");
9798
assertNull(issPeripheral.get().getRootCa(), "Root CA should be NULL");
9899

99-
SCCCredentials sccCredentials = CredentialsFactory.createSCCCredentials("U123", "not so secret");
100+
HubSCCCredentials sccCredentials = CredentialsFactory.createHubSCCCredentials("U123", "not so secret", "fqdn");
100101
CredentialsFactory.storeCredentials(sccCredentials);
101102

102103
peripheral.setRootCa("----- BEGIN CA -----");
@@ -112,7 +113,7 @@ public void testCreateIssPeripheral() {
112113

113114
@Test
114115
public void testCreateIssPeripheralChannels() throws Exception {
115-
SCCCredentials sccCredentials = CredentialsFactory.createSCCCredentials("U123", "not so secret");
116+
HubSCCCredentials sccCredentials = CredentialsFactory.createHubSCCCredentials("U123", "not so secret", "fqdn");
116117
CredentialsFactory.storeCredentials(sccCredentials);
117118

118119
Channel baseChannel = ChannelFactoryTest.createBaseChannel(user);

java/code/src/com/suse/manager/model/hub/test/HubManagerTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.redhat.rhn.common.conf.Config;
2323
import com.redhat.rhn.common.conf.ConfigDefaults;
2424
import com.redhat.rhn.domain.credentials.CredentialsFactory;
25+
import com.redhat.rhn.domain.credentials.HubSCCCredentials;
2526
import com.redhat.rhn.domain.credentials.SCCCredentials;
2627
import com.redhat.rhn.domain.iss.IssRole;
2728
import com.redhat.rhn.manager.setup.MirrorCredentialsManager;
@@ -277,6 +278,38 @@ public void canUpdateServer() {
277278
assertEquals("password", issHub.get().getMirrorCredentials().getPassword());
278279
}
279280

281+
@Test
282+
public void canGenerateSCCCredentials() {
283+
String peripheralFqdn = "dummy.peripheral.fqdn";
284+
285+
var peripheral = (IssPeripheral) hubManager.saveNewServer(IssRole.PERIPHERAL, peripheralFqdn, null);
286+
287+
// Ensure no credentials exists
288+
assertEquals(0, CredentialsFactory.listCredentialsByType(HubSCCCredentials.class).stream()
289+
.filter(creds -> peripheralFqdn.equals(creds.getPeripheralUrl()))
290+
.count());
291+
292+
HubSCCCredentials hubSCCCredentials = hubManager.generateSCCCredentials(peripheral);
293+
assertEquals("peripheral-%06d".formatted(peripheral.getId()), hubSCCCredentials.getUsername());
294+
assertNotNull(hubSCCCredentials.getPassword());
295+
assertEquals(peripheralFqdn, hubSCCCredentials.getPeripheralUrl());
296+
}
297+
298+
@Test
299+
public void canStoreSCCCredentials() {
300+
String hubFqdn = "dummy.hub.fqdn";
301+
var hub = (IssHub) hubManager.saveNewServer(IssRole.HUB, hubFqdn, null);
302+
303+
// Ensure no credentials exists
304+
assertEquals(0, CredentialsFactory.listSCCCredentials().stream()
305+
.filter(creds -> "https://dummy.hub.fqdn".equals(creds.getUrl()))
306+
.count());
307+
308+
SCCCredentials sccCredentials = hubManager.storeSCCCredentials(hub, "dummy-username", "dummy-password");
309+
assertEquals("dummy-username", sccCredentials.getUsername());
310+
assertEquals("dummy-password", sccCredentials.getPassword());
311+
assertEquals("https://dummy.hub.fqdn", sccCredentials.getUrl());
312+
}
280313

281314
@Test
282315
public void canRegisterPeripheralWithUserNameAndPassword()

0 commit comments

Comments
 (0)