feat(api): implement self-update mechanism for Linux agents
- Added a `maybe_self_update` function in `evofw-firewall.sh` to allow agents to pull the latest version of the sync script from the server, enhancing the agent's ability to stay updated. - Updated the `/v1/agent/sync-script` endpoint to return ETag and script SHA256 headers, enabling efficient caching and conditional requests. - Modified the agent policy response to include `script_sha256`, providing visibility into the current version of the sync script. - Enhanced tests to verify the self-update functionality and ensure correct behavior of the sync script endpoint. These changes improve the maintainability and reliability of Linux agents by enabling automatic updates of critical scripts.
This commit is contained in:
@@ -193,6 +193,7 @@ describe('install-links', () => {
|
||||
policy_mode: string
|
||||
apply_version: number
|
||||
hash: string
|
||||
script_sha256: string
|
||||
}
|
||||
expect(body.deny_cidrs).toEqual([])
|
||||
expect(body.allow_cidrs).toEqual([])
|
||||
@@ -200,6 +201,7 @@ describe('install-links', () => {
|
||||
expect(body.policy_mode).toBe('blacklist')
|
||||
expect(body.apply_version).toBe(3)
|
||||
expect(body.hash).toMatch(/^sha256:/)
|
||||
expect(body.script_sha256).toMatch(/^[a-f0-9]{64}$/)
|
||||
|
||||
const agents = await app.inject({ method: 'GET', url: '/api/v1/agents' })
|
||||
const row = (
|
||||
@@ -259,4 +261,70 @@ describe('install-links', () => {
|
||||
expect(rsc.body).toContain('EVOFW_DENY')
|
||||
expect(rsc.body).toContain('203.0.113.0/24')
|
||||
})
|
||||
|
||||
it('sync-script serves ETag and 304 on If-None-Match', async () => {
|
||||
const app = await appPromise
|
||||
await app.ready()
|
||||
|
||||
const first = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/agent/sync-script',
|
||||
})
|
||||
expect(first.statusCode).toBe(200)
|
||||
expect(first.body.startsWith('#!')).toBe(true)
|
||||
expect(first.body).toContain('maybe_self_update')
|
||||
const etag = String(first.headers.etag ?? '')
|
||||
const sha = String(first.headers['x-evofw-script-sha256'] ?? '')
|
||||
expect(etag).toMatch(/^"[a-f0-9]{64}"$/)
|
||||
expect(sha).toBe(etag.replaceAll('"', ''))
|
||||
|
||||
const cached = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/agent/sync-script',
|
||||
headers: { 'if-none-match': etag },
|
||||
})
|
||||
expect(cached.statusCode).toBe(304)
|
||||
|
||||
const miss = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/agent/sync-script',
|
||||
headers: { 'if-none-match': '"deadbeef"' },
|
||||
})
|
||||
expect(miss.statusCode).toBe(200)
|
||||
expect(miss.body).toBe(first.body)
|
||||
|
||||
const created = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/install-links',
|
||||
payload: { name: 'script-sha-policy', platform: 'linux' },
|
||||
})
|
||||
const link = created.json() as { id: string; agent_id: string }
|
||||
const token = 'evofw_script_sha_token_abcdefghij'
|
||||
const enroll = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/agent/enroll',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'x-evofw-seed': 'test-seed',
|
||||
},
|
||||
payload: {
|
||||
name: 'script-sha-policy',
|
||||
platform: 'linux',
|
||||
token,
|
||||
install_link_id: link.id,
|
||||
},
|
||||
})
|
||||
expect(enroll.statusCode).toBe(201)
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: `/api/v1/agents/${link.agent_id}/approve`,
|
||||
})
|
||||
const policy = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/v1/agent/policy',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
})
|
||||
expect(policy.statusCode).toBe(200)
|
||||
expect((policy.json() as { script_sha256: string }).script_sha256).toBe(sha)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user