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

feat(credential-provider-ini): add ignoreCache option #6856

Merged
merged 6 commits into from
Feb 3, 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
51 changes: 51 additions & 0 deletions packages/credential-provider-ini/src/fromIni.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,55 @@ describe(fromIni.name, () => {
mockInitWithParentClientConfig
);
});

describe("ignoreCache option", () => {
it("passes ignoreCache option to parseKnownFiles when true", async () => {
const initWithIgnoreCache = { ...mockInit, ignoreCache: true };
const expectedInitWithParentClientConfig = {
...mockInitWithParentClientConfig,
ignoreCache: true,
};

await fromIni(initWithIgnoreCache)();

expect(parseKnownFiles).toHaveBeenCalledWith(expectedInitWithParentClientConfig);
});

it("passes ignoreCache option to parseKnownFiles when false", async () => {
const initWithIgnoreCache = { ...mockInit, ignoreCache: false };
const expectedInitWithParentClientConfig = {
...mockInitWithParentClientConfig,
ignoreCache: false,
};

await fromIni(initWithIgnoreCache)();

expect(parseKnownFiles).toHaveBeenCalledWith(expectedInitWithParentClientConfig);
});

it("does not pass ignoreCache when option is undefined", async () => {
await fromIni(mockInit)();

expect(parseKnownFiles).toHaveBeenCalledWith(mockInitWithParentClientConfig);
expect(mockInitWithParentClientConfig).not.toHaveProperty("ignoreCache");
});

it("preserves ignoreCache when merging with callerClientConfig", async () => {
const initWithIgnoreCache = { ...mockInit, ignoreCache: true };
const callerConfig = {
profile: "otherProfile",
region: async () => "us-east-1",
};

await fromIni(initWithIgnoreCache)({ callerClientConfig: callerConfig });

expect(parseKnownFiles).toHaveBeenCalledWith(
expect.objectContaining({
ignoreCache: true,
profile: mockProfileName,
parentClientConfig: expect.objectContaining(callerConfig),
})
);
});
});
});
6 changes: 6 additions & 0 deletions packages/credential-provider-ini/src/fromIni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export interface FromIniInit extends SourceProfileInit, CredentialProviderOption
clientConfig?: any;

clientPlugins?: Pluggable<any, any>[];

/**
* When true, always reload credentials from the file system instead of using cached values.
* This is useful when you need to detect changes to the credentials file.
*/
ignoreCache?: boolean;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions supplemental-docs/CLIENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,43 @@ const client = new S3Client({
});
```

#### Enabling uncached/refreshed credentials in `fromIni` credential provider

`fromIni` credential provider accepts a boolean `ignoreCache` option which when true, always reloads credentials from the file system instead of using cached values. This is useful when you need to detect changes to the credentials file.

Note: For temporary credentials that need regular refreshing, consider using `fromTemporaryCredentials` instead.

Using ignoreCache with an S3 client:

```typescript
import { S3Client } from "@aws-sdk/client-s3";
import { fromIni } from "@aws-sdk/credential-providers";

// Create client with credentials that will reload from file
const client = new S3Client({
credentials: fromIni({ ignoreCache: true }),
});
```

For temporary credentials:

You can use the `fromTemporaryCredentials` provider that creates a credential provider function that retrieves temporary credentials from STS AssumeRole API. Depending on your use-case, this might be the preferred way to use temporary credentials, as compared to having a `.ini` file with `ignoreCache` (that will utilize filesystem operations) set to true.

```typescript
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers";

// Better approach for temporary credentials that need regular refreshing
const client = new S3Client({
credentials: fromTemporaryCredentials({
// your temporary credentials config
}),
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this preferred?

Copy link
Contributor Author

@siddsriv siddsriv Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to have the .ini file at all in that case (no filesystem operations), credentials obtained from STS directly

```

- When using with AWS clients, the credential provider function is handled automatically.
- For temporary credentials that need regular refreshing, `fromTemporaryCredentials` is recommended over manual refresh with `ignoreCache`.
- Creating a new client instance ensures fresh credentials.

### AWS Profile `profile`

Available since [v3.714.0](https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.714.0).
Expand Down
Loading