Skip to content

Fix expiration in past warning from ProfileFileRefresher. #6157

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

Merged
merged 7 commits into from
Jun 5, 2025
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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-8565660.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Fix expiration in past warning during profile credential loading."
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class ProfileFileRefresher {
private static final ProfileFileRefreshRecord EMPTY_REFRESH_RECORD = ProfileFileRefreshRecord.builder()
.refreshTime(Instant.MIN)
.build();
private static final long STALE_TIME_MS = 1000;
private final CachedSupplier<ProfileFileRefreshRecord> profileFileCache;
private volatile ProfileFileRefreshRecord currentRefreshRecord;
private final Supplier<ProfileFile> profileFile;
Expand Down Expand Up @@ -96,7 +97,7 @@ private RefreshResult<ProfileFileRefreshRecord> reloadAsRefreshResultIfStale() {
refreshRecord = currentRefreshRecord;
}

return wrapIntoRefreshResult(refreshRecord, now);
return wrapIntoRefreshResult(refreshRecord, now.plusMillis(STALE_TIME_MS));
}

private <T> RefreshResult<T> wrapIntoRefreshResult(T value, Instant staleTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import java.time.ZoneOffset;
import java.time.temporal.TemporalAmount;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.logging.log4j.Level;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.testutils.LogCaptor;

public class ProfileFileRefresherTest {

Expand Down Expand Up @@ -63,43 +65,47 @@ void refreshIfStale_profileModifiedNoPathSpecified_doesNotReloadProfileFile() {
ProfileFileRefresher refresher = refresherWithClock(clock)
.profileFile(() -> profileFile(credentialsFilePath))
.build();
Duration intervalWithinJitter = Duration.ofMillis(100);
Duration intervalWithinStale = Duration.ofMillis(100);

ProfileFile file1 = refresher.refreshIfStale();

generateTestCredentialsFile("modifiedAccessKey", "modifiedSecretAccessKey");
updateModificationTime(credentialsFilePath, clock.instant().plusMillis(1));

clock.tickForward(intervalWithinJitter);
clock.tickForward(intervalWithinStale);
ProfileFile file2 = refresher.refreshIfStale();

Assertions.assertThat(file2).isSameAs(file1);
}

@Test
void refreshIfStale_profileModifiedWithinJitterPeriod_doesNotReloadProfileFile() {
Path credentialsFilePath = generateTestCredentialsFile("defaultAccessKey", "defaultSecretAccessKey");
void refreshIfStale_profileModifiedWithinStalePeriod_doesNotReloadProfileFile() {
try (LogCaptor logCaptor = LogCaptor.create(Level.WARN)) {
Path credentialsFilePath = generateTestCredentialsFile("defaultAccessKey", "defaultSecretAccessKey");

AdjustableClock clock = new AdjustableClock();
ProfileFileRefresher refresher = refresherWithClock(clock)
.profileFile(() -> profileFile(credentialsFilePath))
.profileFilePath(credentialsFilePath)
.build();
Duration intervalWithinJitter = Duration.ofMillis(100);
AdjustableClock clock = new AdjustableClock();
ProfileFileRefresher refresher = refresherWithClock(clock)
.profileFile(() -> profileFile(credentialsFilePath))
.profileFilePath(credentialsFilePath)
.build();
Duration intervalWithinStale = Duration.ofMillis(100);

ProfileFile file1 = refresher.refreshIfStale();
ProfileFile file1 = refresher.refreshIfStale();

clock.tickForward(intervalWithinJitter);
generateTestCredentialsFile("modifiedAccessKey", "modifiedSecretAccessKey");
updateModificationTime(credentialsFilePath, clock.instant());
clock.tickForward(intervalWithinStale);
generateTestCredentialsFile("modifiedAccessKey", "modifiedSecretAccessKey");
updateModificationTime(credentialsFilePath, clock.instant());

ProfileFile file2 = refresher.refreshIfStale();
ProfileFile file2 = refresher.refreshIfStale();

Assertions.assertThat(file2).isSameAs(file1);
Assertions.assertThat(file2).isSameAs(file1);

Assertions.assertThat(logCaptor.loggedEvents()).isEmpty();
}
}

@Test
void refreshIfStale_profileModifiedOutsideJitterPeriod_reloadsProfileFile() {
void refreshIfStale_profileModifiedOutsideStalePeriod_reloadsProfileFile() {
Path credentialsFilePath = generateTestCredentialsFile("defaultAccessKey", "defaultSecretAccessKey");

AdjustableClock clock = new AdjustableClock();
Expand Down
Loading