Docs Menu
Docs Home
/ / /
Node.js
/ /

์—ฐ๊ฒฐ์—์„œ TLS ํ™œ์„ฑํ™”

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ๊ฐœ์š”
  • TLS ํ™œ์„ฑํ™”
  • ์ธ์ฆ์„œ ๊ตฌ์„ฑ
  • ํด๋ผ์ด์–ธํŠธ์˜ ์ฐธ์กฐ ์ธ์ฆ์„œ
  • ์ธ์ฆ์„œ๋ฅผ ์ €์žฅํ•  SecureContext ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ
  • ์ธ์ฆ์„œ ํŒŒ์ผ ๊ฒฝ๋กœ ์ œ๊ณต
  • ์ธ์ฆ์„œ๋ฅผ ์ €์žฅํ•  ๋ฒ„ํผ ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ
  • SecureContext ์˜ˆ์‹œ
  • ์ถ”๊ฐ€ ์ •๋ณด
  • API ๋ฌธ์„œ

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” TLS ๋ณด์•ˆ ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•ด MongoDB ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

TLS๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์—ฐ๊ฒฐ์„ ๊ตฌ์„ฑํ•˜๋ ค๋ฉด TLS ์˜ต์…˜์„ ํ™œ์„ฑํ™”ํ•˜๊ณ  ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์œ„ํ•ด ์ธ์ฆ์„œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

ํŒ

TLS์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ „์†ก ๊ณ„์ธต ๋ณด์•ˆ์— ๋Œ€ํ•œ Wikipedia ํ•ญ๋ชฉ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ MongoDB ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ์—์„œ TLS๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  • MongoClientOptions ๊ฐ์ฒด์—์„œtls ์˜ต์…˜์„ true(์œผ)๋กœ ์„ค์ •

  • ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์—์„œ tls ์˜ต์…˜์„ true(์œผ)๋กœ ์„ค์ •

์ฐธ๊ณ 

์—ฐ๊ฒฐ ๋ฌธ์ž์—ด์— +srv ์ˆ˜์ •์„ ์ง€์ •ํ•˜์—ฌ MongoDB์— ์—ฐ๊ฒฐํ•  ๋•Œ DNS SRV ๊ธฐ๋ก์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฐ๊ฒฐ์—์„œ TLS๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜๋ ค๋ฉด ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด ๋˜๋Š” MongoClientOptions ๊ฐ์ฒด์—์„œ tls ๋˜๋Š” ssl ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์„ false๋กœ ์„ค์ •ํ•˜์„ธ์š”.

DNS ์‹œ๋“œ ๋ชฉ๋ก์„ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์—ฐ๊ฒฐ ๋™์ž‘์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์„œ๋ฒ„ ๋งค๋‰ด์–ผ์˜ SRV ์—ฐ๊ฒฐ ํ˜•์‹ ์„น์…˜์„ ์ฐธ์กฐํ•˜์„ธ์š”.

tls ํด๋ผ์ด์–ธํŠธ ์˜ต์…˜ ์™ธ์—๋„ ๋“œ๋ผ์ด๋ฒ„๋Š” ์—ฐ๊ฒฐ์—์„œ TLS๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ถ”๊ฐ€ ์˜ต์…˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ๋ชฉ์ ์œผ๋กœ tlsAllowInvalidHostnames, tlsAllowInvalidCertificates ๋ฐ tlsInsecure ํด๋ผ์ด์–ธํŠธ ์˜ต์…˜์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

tlsAllowInvalidHostnames ์˜ต์…˜์„ true๋กœ ์„ค์ •ํ•˜๋ฉด ํ˜ธ์ŠคํŠธ ์ด๋ฆ„ ํ™•์ธ์ด ๋น„ํ™œ์„ฑํ™”๋˜๋ฉฐ, tlsAllowInvalidCertificates๋ฅผ true๋กœ ์„ค์ •ํ•˜๋ฉด ์ธ์ฆ์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ๋น„ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค. tlsInsecure์˜ต์…˜์„ true๋กœ ์„ค์ •ํ•˜๋ฉด ์ธ์ฆ์„œ ๋ฐ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ๋ชจ๋‘ ๋น„ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฝ๊ณ 

ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ ์ด๋Ÿฌํ•œ ์˜ต์…˜์„ ์ง€์ •ํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์•ˆ์ „ํ•˜์ง€ ์•Š๊ณ  ๋งŒ๋ฃŒ๋œ ์ธ์ฆ์„œ ๋ฐ ์œ ํšจํ•œ ํด๋ผ์ด์–ธํŠธ ์ธ์Šคํ„ด์Šค๋กœ ์œ„์žฅํ•˜๋Š” ์™ธ๋ถ€ ํ”„๋กœ์„ธ์Šค์— ์ž ์žฌ์ ์œผ๋กœ ์ทจ์•ฝํ•ด์ง‘๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ ์˜ต์…˜์˜ ์ „์ฒด ๋ชฉ๋ก์€ ์—ฐ๊ฒฐ ์˜ต์…˜์„ ์ฐธ์กฐํ•˜์„ธ์š”.

TLS ์š”์ฒญ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์•”ํ˜ธํ™” ์ธ์ฆ์„œ๋ฅผ ์ฐธ์กฐํ•ด ID๋ฅผ ์ฆ๋ช…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. TLS๋กœ MongoDB์— ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด ์ธ์ฆ์„œ๋ฅผ PEM ํŒŒ์ผ๋กœ ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ค‘์š”

ํ”„๋กœ๋•์…˜ ์šฉ๋„์˜ ๊ฒฝ์šฐ MongoDB deployment๋Š” ๋™์ผํ•œ ์ธ์ฆ ๊ธฐ๊ด€์—์„œ ์ƒ์„ฑํ•˜๊ณ  ์„œ๋ช…ํ•œ ์œ ํšจํ•œ ์ธ์ฆ์„œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์ž์ฒด ์„œ๋ช…๋œ ์ธ์ฆ์„œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ๋ชฉ๋ก์—์„œ๋Š” TLS์™€์˜ ์—ฐ๊ฒฐ์„ ์„ค์ •ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ตฌ์„ฑ ์š”์†Œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

TLS ๊ตฌ์„ฑ ์š”์†Œ
์„ค๋ช…
์ธ์ฆ ๊ธฐ๊ด€(CA)
TLS ์—ฐ๊ฒฐ์„ ๋งŒ๋“ค ๋•Œ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜ ์ธ์ฆ ๊ธฐ๊ด€์ž…๋‹ˆ๋‹ค.
ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์„œ
์„œ๋ฒ„๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹ ์›์„ ํ™•์ธํ•˜์—ฌ ์•”ํ˜ธํ™”๋œ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋””์ง€ํ„ธ ์ธ์ฆ์„œ ๋ฐ ํ‚ค์ž…๋‹ˆ๋‹ค.
์ธ์ฆ์„œ ํ‚ค
ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์„œ ๋น„๊ณต๊ฐœ ํ‚ค ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํ‚ค๋Š” ์ธ์ฆ์„œ ํŒŒ์ผ ์ž์ฒด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.
์•”ํ˜ธ ๊ตฌ๋ฌธ
๊ฐœ์ธ ํด๋ผ์ด์–ธํŠธ ํ‚ค๊ฐ€ ์•”ํ˜ธํ™”๋œ ๊ฒฝ์šฐ ๋ณตํ˜ธํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ฒฐ๋˜๊ธฐ ์ „์— ์„œ๋ฒ„์—์„œ ์ธ์ฆ์„œ๋ฅผ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋„๋ก MongoClientOptions ๊ฐ์ฒด์˜ ์ธ์ฆ์„œ๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์ฆ์„œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ธ์ฆ์„œ๋ฅผ ์ €์žฅํ•  SecureContext ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ(๊ถŒ์žฅ)

  • ์ธ์ฆ์„œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฌธ์ž์—ด์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

  • ์ธ์ฆ์„œ๋ฅผ ์ €์žฅํ•  Buffer ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

TLS ์—ฐ๊ฒฐ์„ ๊ตฌ์„ฑํ•˜๋ ค๋ฉด secureContext ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. SecureContext ๊ฐ์ฒด๋Š” Node.js ๊ณ ์œ ์˜ ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ชจ๋“  TLS ์˜ต์…˜์„ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ๊ฐ์ฒด์— ๋ณด๊ด€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SecureContext ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด tls ๋ชจ๋“ˆ์—์„œ createSecureContext() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ, createSecureContext() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์˜ต์…˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ธ์ฆ์„œ ๋‚ด์šฉ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” MongoClientOptions ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” SecureContext ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋Š” SecureContext ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ ์ค๋‹ˆ๋‹ค:

// Create a SecureContext object
const secureContext = tls.createSecureContext({
ca: fs.readFileSync(`<path to CA certificate>`),
cert: fs.readFileSync(`<path to public client certificate>`),
key: fs.readFileSync(`<path to private client key>`),
});
// Pass the SecureContext as a client option
const client = new MongoClient(uri, { tls: true, secureContext });

createSecureContext() ๋ฉ”์„œ๋“œ ๋ฐ tls ํŒจํ‚ค์ง€์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด Node.js TLS API ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

SecureContext ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์˜ˆ์‹œ๋Š” SecureContext ์˜ˆ์‹œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

TLS์— ์—ฐ๊ฒฐํ•˜๋Š” ๋™์•ˆ ์ธ์ฆ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ํด๋ผ์ด์–ธํŠธ ์˜ต์…˜์œผ๋กœ ์ธ์ฆ์„œ์˜ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋Š” MongoClient์—์„œ ์ธ์ฆ์„œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์˜ต์…˜์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

// Pass filepaths as client options
const client = new MongoClient(uri, {
tls: true,
tlsCAFile: `<path to CA certificate>`,
tlsCertificateFile: `<path to public client certificate>`,
tlsCertificateKeyFile: `<path to private client key>`,
});

TLS์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํด๋ผ์ด์–ธํŠธ ์˜ต์…˜์—์„œ ์ธ์ฆ์„œ ํŒŒ์ผ์˜ ์ฝ˜ํ…์ธ ๋ฅผ Buffer ๊ฐ์ฒด๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋Š” ์ธ์ฆ์„œ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ๊ณ  ๊ฒฐ๊ณผ Buffer ๊ฐ์ฒด๋ฅผ MongoClient ์˜ต์…˜์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

// Read file contents
const ca = fs.readFileSync(`<path to CA certificate>`);
const cert = fs.readFileSync(`<path to public client certificate>`);
const key = fs.readFileSync(`<path to private client key>`);
// Pass Buffers as client options
const client = new MongoClient(uri, { tls: true, ca, cert, key });

์ด ์˜ˆ์‹œ์—์„œ๋Š” TLS ์˜ต์…˜์„ ํฌํ•จํ•˜๋Š” SecureContext ๊ฐ์ฒด์™€ MongoClient ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ ์ค๋‹ˆ๋‹ค. ์ด ์˜ˆ์‹œ์—์„œ๋Š” MongoDB์— ์—ฐ๊ฒฐํ•˜์—ฌ ์ฐพ๊ธฐ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

import { MongoClient } from "mongodb";
import * as fs from "fs";
import * as tls from "tls";
// Replace the uri string with your connection string.
const uri = "<connection uri>";
// Replace the filepaths with your certificate filepaths.
const secureContext = tls.createSecureContext({
ca: fs.readFileSync(`<path to CA certificate>`),
cert: fs.readFileSync(`<path to public client certificate>`),
key: fs.readFileSync(`<path to private client key>`),
});
// Create a client with the secureContext option
const client = new MongoClient(uri, { tls: true, secureContext });
async function run() {
try {
const db = client.db("myDB");
const myColl = db.collection("myColl");
const doc = await myColl.findOne({});
console.log(doc);
} finally {
await client.close();
}
}
run().catch(console.dir);

์—ฐ๊ฒฐ์—์„œ TLS๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ ์„œ๋ฒ„ ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

โ†ย ๋„คํŠธ์›Œํฌ ์••์ถ•
Stable APIย โ†’