Skip to content

Commit 1dbe095

Browse files
committed
feat(credential-provider-ini): add ignoreCache option
1 parent 1b99b4f commit 1dbe095

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

packages/credential-provider-ini/src/fromIni.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,55 @@ describe(fromIni.name, () => {
7272
mockInitWithParentClientConfig
7373
);
7474
});
75+
76+
describe("ignoreCache option", () => {
77+
it("passes ignoreCache option to parseKnownFiles when true", async () => {
78+
const initWithIgnoreCache = { ...mockInit, ignoreCache: true };
79+
const expectedInitWithParentClientConfig = {
80+
...mockInitWithParentClientConfig,
81+
ignoreCache: true,
82+
};
83+
84+
await fromIni(initWithIgnoreCache)();
85+
86+
expect(parseKnownFiles).toHaveBeenCalledWith(expectedInitWithParentClientConfig);
87+
});
88+
89+
it("passes ignoreCache option to parseKnownFiles when false", async () => {
90+
const initWithIgnoreCache = { ...mockInit, ignoreCache: false };
91+
const expectedInitWithParentClientConfig = {
92+
...mockInitWithParentClientConfig,
93+
ignoreCache: false,
94+
};
95+
96+
await fromIni(initWithIgnoreCache)();
97+
98+
expect(parseKnownFiles).toHaveBeenCalledWith(expectedInitWithParentClientConfig);
99+
});
100+
101+
it("does not pass ignoreCache when option is undefined", async () => {
102+
await fromIni(mockInit)();
103+
104+
expect(parseKnownFiles).toHaveBeenCalledWith(mockInitWithParentClientConfig);
105+
expect(mockInitWithParentClientConfig).not.toHaveProperty("ignoreCache");
106+
});
107+
108+
it("preserves ignoreCache when merging with callerClientConfig", async () => {
109+
const initWithIgnoreCache = { ...mockInit, ignoreCache: true };
110+
const callerConfig = {
111+
profile: "otherProfile",
112+
region: async () => "us-east-1",
113+
};
114+
115+
await fromIni(initWithIgnoreCache)({ callerClientConfig: callerConfig });
116+
117+
expect(parseKnownFiles).toHaveBeenCalledWith(
118+
expect.objectContaining({
119+
ignoreCache: true,
120+
profile: mockProfileName,
121+
parentClientConfig: expect.objectContaining(callerConfig),
122+
})
123+
);
124+
});
125+
});
75126
});

packages/credential-provider-ini/src/fromIni.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export interface FromIniInit extends SourceProfileInit, CredentialProviderOption
4747
clientConfig?: any;
4848

4949
clientPlugins?: Pluggable<any, any>[];
50+
51+
/**
52+
* When true, always reload credentials from the file system instead of using cached values.
53+
* This is useful when you need to detect changes to the credentials file.
54+
*/
55+
ignoreCache?: boolean;
5056
}
5157

5258
/**

supplemental-docs/CLIENTS.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,60 @@ const client = new S3Client({
151151
});
152152
```
153153

154+
#### Enabling uncached/refreshed credentials in `fromIni` credential provider
155+
156+
`fromIni` credential provider accepts a boolean `ignoreCache` value 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.
157+
158+
Note: For temporary credentials that need regular refreshing, consider using `fromTemporaryCredentials` instead.
159+
160+
Using ignoreCache with an S3 client:
161+
162+
```typescript
163+
import { S3Client } from "@aws-sdk/client-s3";
164+
import { fromIni } from "@aws-sdk/credential-provider-ini";
165+
166+
// Create client with credentials that will reload from file
167+
const client = new S3Client({
168+
credentials: fromIni({ ignoreCache: true })
169+
});
170+
```
171+
172+
Refreshing credentials for an existing client:
173+
```typescript
174+
import { S3Client } from "@aws-sdk/client-s3";
175+
import { fromIni } from "@aws-sdk/credential-provider-ini";
176+
177+
// Initial client setup
178+
const client = new S3Client({
179+
credentials: fromIni({ ignoreCache: true })
180+
});
181+
182+
// To refresh credentials later:
183+
async function refreshClientCredentials() {
184+
const credProvider = fromIni({ ignoreCache: true });
185+
// Get fresh credentials and update the client
186+
const freshCredentials = await credProvider();
187+
client.config.credentials = freshCredentials;
188+
}
189+
```
190+
191+
For temporary credentials:
192+
```typescript
193+
import { fromTemporaryCredentials } from "@aws-sdk/credential-provider-ini";
194+
195+
// Better approach for temporary credentials that need regular refreshing
196+
const client = new S3Client({
197+
credentials: fromTemporaryCredentials({
198+
// your temporary credentials config
199+
})
200+
});
201+
```
202+
203+
- When using with AWS clients, the credential provider function is handled automatically.
204+
- For temporary credentials that need regular refreshing, `fromTemporaryCredentials` is recommended over manual refresh with `ignoreCache`.
205+
- Creating a new client instance ensures fresh credentials.
206+
207+
154208
### AWS Profile `profile`
155209

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

0 commit comments

Comments
 (0)