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

fix(#9799): add user-agent header to outbound #9818

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

witash
Copy link
Contributor

@witash witash commented Feb 24, 2025

adds user-agent header to outbound
User-Agent headers should include versions, which means a little extra stuff; it tries to get the CHT version in the same way as api/src/services/deploy-info.js, but that code is not directly available from shared-libs, so there is some small duplication.
Added getVersion() to shared-libs/settings. It doesn't quite fit there, but I don't think its worth it to make another separate shared-lib

We could also just omit the version if it adds too much mess, but I think these small additions are ok.

closes #9799

@witash witash requested a review from dianabarsan February 25, 2025 08:33
Copy link
Member

@dianabarsan dianabarsan left a comment

Choose a reason for hiding this comment

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

Such a quick turnaround! nice!
I left some minor suggestions inline.

Comment on lines 101 to 114
const getUserAgent = () => {
if (chtVersion === '') {
return secureSettings.getVersion()
.then(v => {
chtVersion = v;
const platform = os.platform();
const arch = os.arch();
return `${CHT_AGENT}/${v} (${platform},${arch})`;
});
}
const platform = os.platform();
const arch = os.arch();
return Promise.resolve(`${CHT_AGENT}/${chtVersion} (${platform},${arch})`);
};
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of duplication here, which I believe can be simplified like this:

Suggested change
const getUserAgent = () => {
if (chtVersion === '') {
return secureSettings.getVersion()
.then(v => {
chtVersion = v;
const platform = os.platform();
const arch = os.arch();
return `${CHT_AGENT}/${v} (${platform},${arch})`;
});
}
const platform = os.platform();
const arch = os.arch();
return Promise.resolve(`${CHT_AGENT}/${chtVersion} (${platform},${arch})`);
};
const getUserAgent = async () => {
if (!chtVersion) {
chtVersion = await secureSettings.getVersion();
}
const platform = os.platform();
const arch = os.arch();
return `${CHT_AGENT}/${chtVersion} (${platform},${arch})`;
};

sendOptions.headers = {
Authorization: value
};
sendOptions.headers.Authorization = value;
Copy link
Member

Choose a reason for hiding this comment

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

http headers must be lowercase in http2: https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2
I know we're not using http2 everywhere (yet), http1 headers are case-insensitive so might as well respect the stricter standard.

Suggested change
sendOptions.headers.Authorization = value;
sendOptions.headers.authorization = value;


const setupUserAgent = () => {
return getUserAgent().then(userAgent => {
sendOptions.headers['User-Agent'] = userAgent;
Copy link
Member

Choose a reason for hiding this comment

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

http headers must be lowercase in http2: https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2

Suggested change
sendOptions.headers['User-Agent'] = userAgent;
sendOptions.headers['user-agent'] = userAgent;

Comment on lines 197 to 217
return setupUserAgent()
.then(() => auth())
.then(() => {
if (logger.isDebugEnabled()) {
logger.debug('About to send outbound request');
const clone = JSON.parse(JSON.stringify(sendOptions));
if (clone.auth && clone.auth.password) {
// mask password before logging
clone.auth.password = '*****';
}
logger.debug(JSON.stringify(clone, null, 2));
}
logger.debug(JSON.stringify(clone, null, 2));
}

return request.post(sendOptions)
.then(result => {
if (logger.isDebugEnabled()) {
logger.debug('result from outbound request');
logger.debug(JSON.stringify(result, null, 2));
}
});
});
return request.post(sendOptions)
.then(result => {
if (logger.isDebugEnabled()) {
logger.debug('result from outbound request');
logger.debug(JSON.stringify(result, null, 2));
}
});
});
Copy link
Member

Choose a reason for hiding this comment

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

Can you please convert this to async / await?

@@ -128,8 +131,26 @@ const getCouchConfig = (param, nodeName) => {
}
};

const getVersion = () => {
Copy link
Member

Choose a reason for hiding this comment

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

Please convert this to async / await.
Also I believe we should not be duplicating this code, we've had issues around versioning and I'm not comfortable keeping two sections that will return the version.

Can you maybe pass the version (which is static) to the outbound shared lib when you initialize it? Or move this code somewhere where it makes sense (my best idea here is the environment shared library), and call that in API as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuring headers in outbound push
2 participants