-
Notifications
You must be signed in to change notification settings - Fork 61
fix: prefer proxy password from env var #385
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
Changes from 5 commits
497f0c5
a057e5a
725bf4c
24b1b36
be9f532
d0e41d4
288f8e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -260,7 +260,27 @@ export class ProxyConfiguration extends CoreProxyConfiguration { | |||||
| */ | ||||||
| async initialize(): Promise<boolean> { | ||||||
| if (this.usesApifyProxy) { | ||||||
| await this._setPasswordIfToken(); | ||||||
| if (!this.password) { | ||||||
| await this._setPasswordIfToken(); | ||||||
| } | ||||||
|
|
||||||
| if (!this.password) { | ||||||
| if (Actor.isAtHome()) { | ||||||
| throw new Error( | ||||||
| `Apify Proxy password must be provided using options.password or the "${APIFY_ENV_VARS.PROXY_PASSWORD}" environment variable. ` + | ||||||
| `If you add the "${APIFY_ENV_VARS.TOKEN}" environment variable, ` + | ||||||
| `while not providing "${APIFY_ENV_VARS.PROXY_PASSWORD}", the password will be automatically inferred.`, | ||||||
| ); | ||||||
| } else { | ||||||
| this.log.warning( | ||||||
| `No proxy password or token detected, running without proxy. To use Apify Proxy locally, ` + | ||||||
| `provide options.password or "${APIFY_ENV_VARS.PROXY_PASSWORD}" environment variable. ` + | ||||||
| `If you add the "${APIFY_ENV_VARS.TOKEN}" environment variable, ` + | ||||||
| `while not providing "${APIFY_ENV_VARS.PROXY_PASSWORD}", the password will be automatically inferred.`, | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return this._checkAccess(); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -415,8 +435,9 @@ export class ProxyConfiguration extends CoreProxyConfiguration { | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks if Apify Token is provided in env and gets the password via API and sets it to env | ||||||
| * Checks if proxy password is provided in env, if not, fetches it from API using Apify Token | ||||||
|
||||||
| * Checks if proxy password is provided in env, if not, fetches it from API using Apify Token | |
| * Fetch & set the proxy password from Apify API if an Apify token is provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, my bad. Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the method name and the documentation make sense anymore.
The logic should be as follows:
- If the password is provided through env, use that.
- If not, try to use the token to fetch the password from API as fallback.
Considering the existing implementation, I would:
- Rename this to something like
fetchPasswordFromApi. - Update the initialize function to use this method.
async initialize(): Promise<boolean> {
if (this.usesApifyProxy) {
if (!this.password) {
this.password = await this.fetchPasswordFromApi();
}
if (!this.password) {
// Do the logging here.
}
return this._checkAccess();
}
return true;
IMHO this will capture way better what the logic is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, the method is protected, so technically, renaming it is a BC.
(I guess it should be safe, but still.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's true... well, it's kind of your call here 😄
We could also keep it around, mark it as deprecated, and from within do this.password = fetchPasswordFromApi().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather stay on the safe side and keep it around, marked as deprecated or have a TODO on it so we can rename and make it private in the next major (which we will hopefully get to later this year or early next year).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep the name and make the checks and warnings as @tobice proposed:
async initialize(): Promise<boolean> {
if (this.usesApifyProxy) {
if (!this.password) {
await this._setPasswordIfToken();
}
if (!this.password) {
// Do the logging here.
}
return this._checkAccess();
}
return true;That makes the most sense to me.
Edit: ...and adding TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
B4nan marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) IMHO this whole function can be now simplified.
protected async _setPasswordIfToken(): Promise<void> {
const token = this.config.get('token');
if (!token) {
return;
}
try {
const user = await Actor.apifyClient.user().get();
this.password = user.proxy?.password;
} catch (error) {
if (Actor.isAtHome()) {
throw error;
} else {
this.log.warning(`Failed to fetch user data using token`, { error });
}
}
}
Also disabling proxy is misleading within the scope of this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) Inferred sounds like a mathematical operation. Why not just fetched? 😄
Or even an alternative formulation:
Dtto below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used this with a slight addition.