Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

b/319014043 Fix code analysis nits #292

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public TemporaryIamCondition(@NotNull String condition) {
Instant.parse(matcher.group(1)),
Instant.parse(matcher.group(2)));
}
catch (DateTimeParseException e) {}
catch (DateTimeParseException ignored) {}
}

throw new IllegalArgumentException("Condition is not a temporary IAM condition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public record EntitlementSet<TId extends EntitlementId>(
//
var availableAndInactive = availableEntitlements
.stream()
.filter(ent -> !validActivations
.filter(ent -> validActivations
.stream()
.anyMatch(active -> active.entitlementId().equals(ent.id())))
.noneMatch(active -> active.entitlementId().equals(ent.id())))
.collect(Collectors.toCollection(TreeSet::new));

assert availableAndInactive.stream().noneMatch(e -> validActivations.contains(e.id()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package com.google.solutions.jitaccess.core.catalog;

import com.google.solutions.jitaccess.core.UserEmail;
import jakarta.validation.constraints.Null;
import org.jetbrains.annotations.NotNull;

/**
* Policy for verifying justification messages.
Expand All @@ -31,8 +33,8 @@ public interface JustificationPolicy {
* Check that a justification meets criteria.
*/
void checkJustification(
UserEmail user,
String justification
@NotNull UserEmail user,
@Null String justification
) throws InvalidJustificationException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.base.Strings;
import com.google.solutions.jitaccess.core.UserEmail;
import jakarta.inject.Singleton;
import jakarta.validation.constraints.Null;
import org.jetbrains.annotations.NotNull;

import java.util.regex.Pattern;
Expand All @@ -43,8 +44,8 @@ public RegexJustificationPolicy(@NotNull Options options) {

@Override
public void checkJustification(
UserEmail user,
@NotNull String justification
@NotNull UserEmail user,
@Null String justification
) throws InvalidJustificationException {
if (
Strings.isNullOrEmpty(justification) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static <T> T awaitAndRethrow(@NotNull CompletableFuture<T> future) throws Access
this.executor);

var principalSetForUser = new PrincipalSet(user, awaitAndRethrow(listMembershipsFuture));
var allBindings = awaitAndRethrow(effectivePoliciesFuture)
return awaitAndRethrow(effectivePoliciesFuture)
.stream()

// All bindings, across all resources in the ancestry.
Expand All @@ -124,7 +124,6 @@ static <T> T awaitAndRethrow(@NotNull CompletableFuture<T> future) throws Access
// Only bindings that apply to the user.
.filter(binding -> principalSetForUser.isMember(binding))
.collect(Collectors.toList());
return allBindings;
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -204,8 +203,7 @@ public SortedSet<ProjectId> findProjectsWithEntitlements(
allAvailable.addAll(jitEligible);
allAvailable.addAll(mpaEligible
.stream()
.filter(r -> !jitEligible.stream().anyMatch(a -> a.id().equals(r.id())))
.collect(Collectors.toList()));
.filter(r -> jitEligible.stream().noneMatch(a -> a.id().equals(r.id()))).toList());
}

//
Expand All @@ -217,8 +215,7 @@ public SortedSet<ProjectId> findProjectsWithEntitlements(

for (var binding : allBindings.stream()
// Only temporary access bindings.
.filter(binding -> JitConstraints.isActivated(binding.getCondition()))
.collect(Collectors.toUnmodifiableList()))
.filter(binding -> JitConstraints.isActivated(binding.getCondition())).toList())
{
var condition = new TemporaryIamCondition(binding.getCondition().getExpression());
boolean isValid;
Expand Down Expand Up @@ -301,15 +298,14 @@ public SortedSet<ProjectId> findProjectsWithEntitlements(
}
},
this.executor))
.collect(Collectors.toList());
.toList();

var allMembers = new HashSet<>(allUserMembers);

for (var listMembersFuture : listMembersFutures) {
var members = awaitAndRethrow(listMembersFuture)
.stream()
.map(m -> new UserEmail(m.getEmail()))
.collect(Collectors.toList());
.map(m -> new UserEmail(m.getEmail())).toList();
allMembers.addAll(members);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ private record ConditionalRoleBinding(RoleBinding binding, Expr condition) {}
allAvailable.addAll(jitEligible);
allAvailable.addAll(mpaEligible
.stream()
.filter(r -> !jitEligible.stream().anyMatch(a -> a.id().equals(r.id())))
.collect(Collectors.toList()));
.filter(r -> jitEligible.stream().noneMatch(a -> a.id().equals(r.id())))
.toList());
}

var allActive = new HashSet<EntitlementSet.ActivatedEntitlement<ProjectRoleBinding>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public JsonWebToken.Payload convert(@NotNull MpaActivationRequest<ProjectRoleBin
.set("end", request.endTime().getEpochSecond());
}

@SuppressWarnings("unchecked")
@Override
public @NotNull MpaActivationRequest<ProjectRoleBinding> convert(JsonWebToken.@NotNull Payload payload) {
var roleBinding = new RoleBinding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,7 @@ public static boolean equals(@NotNull Binding lhs, @NotNull Binding rhs, boolean
return false;
}

if (!Objects.equals(lhs.getCondition().getDescription(), rhs.getCondition().getDescription())) {
return false;
}
return Objects.equals(lhs.getCondition().getDescription(), rhs.getCondition().getDescription());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ public class RuntimeEnvironment {
// Private helpers.
// -------------------------------------------------------------------------

private static HttpResponse getMetadata(String path) throws IOException {
GenericUrl genericUrl = new GenericUrl(ComputeEngineCredentials.getMetadataServerUrl() + path);
HttpRequest request = new NetHttpTransport().createRequestFactory().buildGetRequest(genericUrl);
private static HttpResponse getMetadata() throws IOException {
var genericUrl = new GenericUrl(
ComputeEngineCredentials.getMetadataServerUrl() +
"/computeMetadata/v1/project/?recursive=true");

var request = new NetHttpTransport()
.createRequestFactory()
.buildGetRequest(genericUrl);

request.setParser(new JsonObjectParser(GsonFactory.getDefaultInstance()));
request.getHeaders().set("Metadata-Flavor", "Google");
Expand Down Expand Up @@ -136,7 +141,7 @@ public RuntimeEnvironment() {
//
try {
GenericData projectMetadata =
getMetadata("/computeMetadata/v1/project/?recursive=true").parseAs(GenericData.class);
getMetadata().parseAs(GenericData.class);

this.projectId = (String) projectMetadata.get("projectId");
this.projectNumber = projectMetadata.get("numericProjectId").toString();
Expand Down
Loading