Skip to content

Commit c53e239

Browse files
feat: use new permissions model (#546)
* feat: use new permissions model * feat: use new permissions model
1 parent bf90c0b commit c53e239

File tree

7 files changed

+81
-35
lines changed

7 files changed

+81
-35
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ SOFTWARE.
101101
<dependency>
102102
<groupId>com.artipie</groupId>
103103
<artifactId>http</artifactId>
104-
<version>v1.1.4</version>
104+
<version>v1.2.3</version>
105105
</dependency>
106106
<dependency>
107107
<groupId>javax.xml.bind</groupId>
@@ -111,7 +111,7 @@ SOFTWARE.
111111
<dependency>
112112
<groupId>com.artipie</groupId>
113113
<artifactId>asto-core</artifactId>
114-
<version>v1.15.0</version>
114+
<version>v1.15.4</version>
115115
</dependency>
116116
<dependency>
117117
<groupId>commons-cli</groupId>

src/main/java/com/artipie/rpm/CliArguments.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,10 @@ public Optional<String> cron() {
157157
this.cli.getOptionValue(RpmOptions.UPDATE.option().getOpt())
158158
);
159159
}
160+
161+
@Override
162+
public String name() {
163+
throw new UnsupportedOperationException("Method name() is not supported");
164+
}
160165
}
161166
}

src/main/java/com/artipie/rpm/RepoConfig.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public interface RepoConfig {
5050
*/
5151
Optional<String> cron();
5252

53+
/**
54+
* Repository name.
55+
* @return String name
56+
*/
57+
String name();
58+
5359
/**
5460
* Rpm repository update mode.
5561
* @since 1.9
@@ -89,20 +95,28 @@ final class FromYaml implements RepoConfig {
8995
*/
9096
private final YamlMapping yaml;
9197

98+
/**
99+
* Repository name.
100+
*/
101+
private final String name;
102+
92103
/**
93104
* Ctor.
94105
* @param yaml Yaml settings
106+
* @param name Repository name
95107
*/
96-
public FromYaml(final YamlMapping yaml) {
108+
public FromYaml(final YamlMapping yaml, final String name) {
97109
this.yaml = yaml;
110+
this.name = name;
98111
}
99112

100113
/**
101114
* Ctor.
102115
* @param yaml Yaml settings
116+
* @param name Repository name
103117
*/
104-
public FromYaml(final Optional<YamlMapping> yaml) {
105-
this(yaml.orElse(Yaml.createYamlMappingBuilder().build()));
118+
public FromYaml(final Optional<YamlMapping> yaml, final String name) {
119+
this(yaml.orElse(Yaml.createYamlMappingBuilder().build()), name);
106120
}
107121

108122
@Override
@@ -156,6 +170,11 @@ public Optional<String> cron() {
156170
}
157171
return res;
158172
}
173+
174+
@Override
175+
public String name() {
176+
return this.name;
177+
}
159178
}
160179

161180
/**
@@ -249,5 +268,10 @@ public UpdateMode mode() {
249268
public Optional<String> cron() {
250269
return Optional.empty();
251270
}
271+
272+
@Override
273+
public String name() {
274+
return "test";
275+
}
252276
}
253277
}

src/main/java/com/artipie/rpm/http/RpmSlice.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
import com.artipie.asto.Storage;
88
import com.artipie.http.Slice;
9-
import com.artipie.http.auth.Action;
109
import com.artipie.http.auth.Authentication;
11-
import com.artipie.http.auth.BasicAuthSlice;
12-
import com.artipie.http.auth.Permission;
13-
import com.artipie.http.auth.Permissions;
10+
import com.artipie.http.auth.BasicAuthzSlice;
11+
import com.artipie.http.auth.OperationControl;
1412
import com.artipie.http.rq.RqMethod;
1513
import com.artipie.http.rs.StandardRs;
1614
import com.artipie.http.rt.ByMethodsRule;
@@ -20,6 +18,9 @@
2018
import com.artipie.http.slice.SliceDownload;
2119
import com.artipie.http.slice.SliceSimple;
2220
import com.artipie.rpm.RepoConfig;
21+
import com.artipie.security.perms.Action;
22+
import com.artipie.security.perms.AdapterBasicPermission;
23+
import com.artipie.security.policy.Policy;
2324

2425
/**
2526
* Artipie {@link Slice} for RPM repository HTTP API.
@@ -33,47 +34,53 @@ public final class RpmSlice extends Slice.Wrap {
3334
* @param storage The storage.
3435
*/
3536
public RpmSlice(final Storage storage) {
36-
this(storage, Permissions.FREE, Authentication.ANONYMOUS, new RepoConfig.Simple());
37+
this(storage, Policy.FREE, Authentication.ANONYMOUS, new RepoConfig.Simple());
3738
}
3839

3940
/**
4041
* Ctor.
4142
* @param storage Storage
42-
* @param perms Access permissions.
43+
* @param policy Access policy.
4344
* @param auth Auth details.
4445
* @param config Repository configuration.
4546
* @checkstyle ParameterNumberCheck (10 lines)
4647
*/
4748
public RpmSlice(
4849
final Storage storage,
49-
final Permissions perms,
50+
final Policy<?> policy,
5051
final Authentication auth,
5152
final RepoConfig config
5253
) {
5354
super(
5455
new SliceRoute(
5556
new RtRulePath(
5657
new ByMethodsRule(RqMethod.GET),
57-
new BasicAuthSlice(
58+
new BasicAuthzSlice(
5859
new SliceDownload(storage),
5960
auth,
60-
new Permission.ByName(perms, Action.Standard.READ)
61+
new OperationControl(
62+
policy, new AdapterBasicPermission(config.name(), Action.Standard.READ)
63+
)
6164
)
6265
),
6366
new RtRulePath(
6467
new ByMethodsRule(RqMethod.PUT),
65-
new BasicAuthSlice(
68+
new BasicAuthzSlice(
6669
new RpmUpload(storage, config),
6770
auth,
68-
new Permission.ByName(perms, Action.Standard.WRITE)
71+
new OperationControl(
72+
policy, new AdapterBasicPermission(config.name(), Action.Standard.WRITE)
73+
)
6974
)
7075
),
7176
new RtRulePath(
7277
new ByMethodsRule(RqMethod.DELETE),
73-
new BasicAuthSlice(
78+
new BasicAuthzSlice(
7479
new RpmRemove(storage, config),
7580
auth,
76-
new Permission.ByName(perms, Action.Standard.WRITE)
81+
new OperationControl(
82+
policy, new AdapterBasicPermission(config.name(), Action.Standard.READ)
83+
)
7784
)
7885
),
7986
new RtRulePath(RtRule.FALLBACK, new SliceSimple(StandardRs.NOT_FOUND))

src/test/java/com/artipie/rpm/RepoConfigFromYamlTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@ public final class RepoConfigFromYamlTest {
2222

2323
@Test
2424
void readsSettings() {
25+
final String name = "any";
2526
MatcherAssert.assertThat(
2627
new RepoConfig.FromYaml(
2728
Yaml.createYamlMappingBuilder().add("digest", "sha1")
2829
.add("naming-policy", "sha256").add("filelists", "false")
29-
.add("update", Yaml.createYamlMappingBuilder().add("on", "upload").build()).build()
30+
.add("update", Yaml.createYamlMappingBuilder().add("on", "upload").build()).build(),
31+
name
3032
),
3133
new AllOf<>(
3234
new ListOf<Matcher<? super RepoConfig>>(
3335
new Satisfies<>(cfg -> cfg.digest() == Digest.SHA1),
3436
new Satisfies<>(cfg -> cfg.naming() == StandardNamingPolicy.SHA256),
3537
new Satisfies<>(fromYaml -> !fromYaml.filelists()),
3638
new Satisfies<>(cfg -> cfg.mode() == RepoConfig.UpdateMode.UPLOAD),
37-
new Satisfies<>(cfg -> !cfg.cron().isPresent())
39+
new Satisfies<>(cfg -> !cfg.cron().isPresent()),
40+
new Satisfies<>(cfg -> name.equals(cfg.name()))
3841
)
3942
)
4043
);
@@ -43,6 +46,7 @@ void readsSettings() {
4346
@Test
4447
void readsSettingsWithCron() {
4548
final String cron = "0 * * * *";
49+
final String name = "repo1";
4650
MatcherAssert.assertThat(
4751
new RepoConfig.FromYaml(
4852
Yaml.createYamlMappingBuilder()
@@ -52,28 +56,32 @@ void readsSettingsWithCron() {
5256
"on",
5357
Yaml.createYamlMappingBuilder().add("cron", cron).build()
5458
).build()
55-
).build()
59+
).build(),
60+
name
5661
),
5762
new AllOf<>(
5863
new ListOf<Matcher<? super RepoConfig>>(
5964
new Satisfies<>(cfg -> cfg.mode() == RepoConfig.UpdateMode.CRON),
60-
new Satisfies<>(cfg -> cfg.cron().get().equals(cron))
65+
new Satisfies<>(cfg -> cfg.cron().get().equals(cron)),
66+
new Satisfies<>(cfg -> name.equals(cfg.name()))
6167
)
6268
)
6369
);
6470
}
6571

6672
@Test
6773
void returnsDefaults() {
74+
final String name = "test";
6875
MatcherAssert.assertThat(
69-
new RepoConfig.FromYaml(Optional.empty()),
76+
new RepoConfig.FromYaml(Optional.empty(), name),
7077
new AllOf<>(
7178
new ListOf<Matcher<? super RepoConfig>>(
7279
new Satisfies<>(cfg -> cfg.digest() == Digest.SHA256),
7380
new Satisfies<>(cfg -> cfg.naming() == StandardNamingPolicy.SHA256),
7481
new Satisfies<>(RepoConfig::filelists),
7582
new Satisfies<>(cfg -> cfg.mode() == RepoConfig.UpdateMode.UPLOAD),
76-
new Satisfies<>(cfg -> !cfg.cron().isPresent())
83+
new Satisfies<>(cfg -> !cfg.cron().isPresent()),
84+
new Satisfies<>(cfg -> cfg.name().equals(name))
7785
)
7886
)
7987
);

src/test/java/com/artipie/rpm/http/RpmSliceDownloadITCase.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import com.artipie.asto.SubStorage;
1010
import com.artipie.asto.memory.InMemoryStorage;
1111
import com.artipie.http.auth.Authentication;
12-
import com.artipie.http.auth.Permissions;
1312
import com.artipie.http.slice.LoggingSlice;
1413
import com.artipie.rpm.Digest;
1514
import com.artipie.rpm.NamingPolicy;
1615
import com.artipie.rpm.RepoConfig;
1716
import com.artipie.rpm.Rpm;
1817
import com.artipie.rpm.TestRpm;
18+
import com.artipie.security.policy.Policy;
19+
import com.artipie.security.policy.PolicyByUsername;
1920
import com.artipie.vertx.VertxSliceServer;
2021
import io.vertx.reactivex.core.Vertx;
2122
import java.io.IOException;
@@ -92,7 +93,7 @@ void init() {
9293
void installsByUrl() throws Exception {
9394
final TestRpm rpm = new TestRpm.Time();
9495
rpm.put(this.asto);
95-
this.start(Permissions.FREE, Authentication.ANONYMOUS);
96+
this.start(Policy.FREE, Authentication.ANONYMOUS);
9697
MatcherAssert.assertThat(
9798
this.yumInstall(
9899
String.format(
@@ -111,7 +112,7 @@ void installsByUrlWithAuth() throws Exception {
111112
final TestRpm rpm = new TestRpm.Time();
112113
rpm.put(this.asto);
113114
this.start(
114-
new Permissions.Single(john, "download"),
115+
new PolicyByUsername(john),
115116
new Authentication.Single(john, pswd)
116117
);
117118
MatcherAssert.assertThat(
@@ -130,7 +131,7 @@ void installsFromRepoWithSubDirs() throws IOException, InterruptedException {
130131
new TestRpm.Aspell().put(new SubStorage(new Key.From("spelling"), this.asto));
131132
new TestRpm.Time().put(this.asto);
132133
new Rpm(this.asto, RpmSliceDownloadITCase.CONFIG).batchUpdate(Key.ROOT).blockingAwait();
133-
this.start(Permissions.FREE, Authentication.ANONYMOUS);
134+
this.start(Policy.FREE, Authentication.ANONYMOUS);
134135
final Path setting = this.tmp.resolve("example.repo");
135136
this.tmp.resolve("example.repo").toFile().createNewFile();
136137
Files.write(
@@ -174,7 +175,7 @@ private String yumInstall(final String url) throws IOException, InterruptedExcep
174175
).getStdout();
175176
}
176177

177-
private void start(final Permissions perms, final Authentication auth) {
178+
private void start(final Policy<?> perms, final Authentication auth) {
178179
this.server = new VertxSliceServer(
179180
RpmSliceDownloadITCase.VERTX,
180181
new LoggingSlice(new RpmSlice(this.asto, perms, auth, RpmSliceDownloadITCase.CONFIG))

src/test/java/com/artipie/rpm/http/RpmSliceITCase.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import com.artipie.asto.Storage;
99
import com.artipie.asto.memory.InMemoryStorage;
1010
import com.artipie.http.auth.Authentication;
11-
import com.artipie.http.auth.Permissions;
1211
import com.artipie.http.slice.LoggingSlice;
1312
import com.artipie.rpm.Digest;
1413
import com.artipie.rpm.NamingPolicy;
1514
import com.artipie.rpm.RepoConfig;
1615
import com.artipie.rpm.Rpm;
1716
import com.artipie.rpm.TestRpm;
17+
import com.artipie.security.policy.Policy;
18+
import com.artipie.security.policy.PolicyByUsername;
1819
import com.artipie.vertx.VertxSliceServer;
1920
import com.jcabi.log.Logger;
2021
import io.vertx.reactivex.core.Vertx;
@@ -87,7 +88,7 @@ public final class RpmSliceITCase {
8788
})
8889
void canListAndInstallFromArtipieRepo(final String linux,
8990
final String mngr, final String rey) throws Exception {
90-
this.start(Permissions.FREE, Authentication.ANONYMOUS, "", linux);
91+
this.start(Policy.FREE, Authentication.ANONYMOUS, "", linux);
9192
MatcherAssert.assertThat(
9293
"Lists 'time' package",
9394
this.exec(mngr, rey, "list"),
@@ -110,7 +111,7 @@ void canListAndInstallFromArtipieRepoWithAuth(final String linux,
110111
final String mark = "mark";
111112
final String pswd = "abc";
112113
this.start(
113-
new Permissions.Single(mark, "download"),
114+
new PolicyByUsername(mark),
114115
new Authentication.Single(mark, pswd),
115116
String.format("%s:%s@", mark, pswd),
116117
linux
@@ -156,15 +157,15 @@ private String exec(final String mngr, final String key, final String action) th
156157

157158
/**
158159
* Starts VertxSliceServer and docker container.
159-
* @param perms Permissions
160+
* @param policy Permissions
160161
* @param auth Authentication
161162
* @param cred String with user name and password to add in url, uname:pswd@
162163
* @param linux Linux distribution name and version
163164
* @throws Exception On error
164165
* @checkstyle ParameterNumberCheck (10 lines)
165166
* @checkstyle ExecutableStatementCountCheck (100 lines)
166167
*/
167-
private void start(final Permissions perms, final Authentication auth, final String cred,
168+
private void start(final Policy<?> policy, final Authentication auth, final String cred,
168169
final String linux) throws Exception {
169170
final Storage storage = new InMemoryStorage();
170171
new TestRpm.Time().put(storage);
@@ -174,7 +175,7 @@ private void start(final Permissions perms, final Authentication auth, final Str
174175
new Rpm(storage, config).batchUpdate(Key.ROOT).blockingAwait();
175176
this.server = new VertxSliceServer(
176177
RpmSliceITCase.VERTX,
177-
new LoggingSlice(new RpmSlice(storage, perms, auth, config))
178+
new LoggingSlice(new RpmSlice(storage, policy, auth, config))
178179
);
179180
final int port = this.server.start();
180181
Testcontainers.exposeHostPorts(port);

0 commit comments

Comments
 (0)