-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: astro:env validateSecrets (#11337)
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
75d118b
commit 0a4b31f
Showing
5 changed files
with
143 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Adds a new property `experimental.env.validateSecrets` to allow validating private variables on the server. | ||
|
||
By default, this is set to `false` and only public variables are checked on start. If enabled, secrets will also be checked on start (dev/build modes). This is useful for example in some CIs to make sure all your secrets are correctly set before deploying. | ||
|
||
```js | ||
// astro.config.mjs | ||
import { defineConfig, envField } from "astro/config" | ||
|
||
export default defineConfig({ | ||
experimental: { | ||
env: { | ||
schema: { | ||
// ... | ||
}, | ||
validateSecrets: true | ||
} | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,90 @@ | ||
import assert from 'node:assert/strict'; | ||
import { writeFileSync } from 'node:fs'; | ||
import { after, describe, it } from 'node:test'; | ||
import { afterEach, describe, it } from 'node:test'; | ||
import * as cheerio from 'cheerio'; | ||
import testAdapter from './test-adapter.js'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('astro:env public variables', () => { | ||
describe('astro:env secret variables', () => { | ||
/** @type {Awaited<ReturnType<typeof loadFixture>>} */ | ||
let fixture; | ||
/** @type {Awaited<ReturnType<(typeof fixture)["loadTestAdapterApp"]>>} */ | ||
let app; | ||
/** @type {Awaited<ReturnType<(typeof fixture)["startDevServer"]>>} */ | ||
/** @type {Awaited<ReturnType<(typeof fixture)["startDevServer"]>> | undefined} */ | ||
let devServer = undefined; | ||
|
||
describe('Server variables', () => { | ||
after(async () => { | ||
await devServer?.stop(); | ||
afterEach(async () => { | ||
await devServer?.stop(); | ||
if (process.env.KNOWN_SECRET) { | ||
delete process.env.KNOWN_SECRET | ||
} | ||
}); | ||
|
||
it('works in dev', async () => { | ||
process.env.KNOWN_SECRET = '5' | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
const response = await fixture.fetch('/'); | ||
assert.equal(response.status, 200); | ||
}); | ||
|
||
it('works in dev', async () => { | ||
writeFileSync( | ||
new URL('./fixtures/astro-env-server-secret/.env', import.meta.url), | ||
'KNOWN_SECRET=5', | ||
'utf-8' | ||
); | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
const response = await fixture.fetch('/'); | ||
assert.equal(response.status, 200); | ||
it('builds without throwing', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
output: 'server', | ||
adapter: testAdapter({ | ||
env: { | ||
KNOWN_SECRET: '123456', | ||
UNKNOWN_SECRET: 'abc', | ||
}, | ||
}), | ||
}); | ||
await fixture.build(); | ||
assert.equal(true, true); | ||
}); | ||
|
||
it('builds without throwing', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
output: 'server', | ||
adapter: testAdapter({ | ||
env: { | ||
KNOWN_SECRET: '123456', | ||
UNKNOWN_SECRET: 'abc', | ||
}, | ||
}), | ||
}); | ||
await fixture.build(); | ||
app = await fixture.loadTestAdapterApp(); | ||
assert.equal(true, true); | ||
it('adapter can set how env is retrieved', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
output: 'server', | ||
adapter: testAdapter({ | ||
env: { | ||
KNOWN_SECRET: '123456', | ||
UNKNOWN_SECRET: 'abc', | ||
}, | ||
}), | ||
}); | ||
await fixture.build(); | ||
const app = await fixture.loadTestAdapterApp(); | ||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
assert.equal(response.status, 200); | ||
|
||
it('adapter can set how env is retrieved', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
output: 'server', | ||
adapter: testAdapter({ | ||
env: { | ||
KNOWN_SECRET: '123456', | ||
UNKNOWN_SECRET: 'abc', | ||
}, | ||
}), | ||
}); | ||
await fixture.build(); | ||
app = await fixture.loadTestAdapterApp(); | ||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
assert.equal(response.status, 200); | ||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
|
||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
const data = JSON.parse($('#data').text()); | ||
|
||
const data = JSON.parse($('#data').text()); | ||
assert.equal(data.KNOWN_SECRET, 123456); | ||
assert.equal(data.UNKNOWN_SECRET, 'abc'); | ||
}); | ||
|
||
assert.equal(data.KNOWN_SECRET, 123456); | ||
assert.equal(data.UNKNOWN_SECRET, 'abc'); | ||
it('fails if validateSecrets is enabled and secret is not set', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-secret/', | ||
experimental: { | ||
env: { | ||
validateSecrets: true, | ||
}, | ||
}, | ||
}); | ||
|
||
try { | ||
await fixture.build(); | ||
assert.fail() | ||
} catch (error) { | ||
assert.equal(error instanceof Error, true); | ||
assert.equal(error.title, 'Invalid Environment Variables'); | ||
assert.equal(error.message.includes('Variable KNOWN_SECRET is not of type: number.'), true); | ||
} | ||
}); | ||
}); |