0Pricing
Edge Computing with Cloudflare Workers & Deno · บทเรียน

การจัดการข้อมูลลับอย่างปลอดภัย

เรียนรู้แนวทางปฏิบัติที่ดีในการจัดการคีย์ API โทเค็น และข้อมูลละเอียดอ่อนอื่น ๆ อย่างปลอดภัยในสภาพแวดล้อมใกล้ผู้ใช้

การจัดการข้อมูลลับอย่างปลอดภัย เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Edge Computing with Cloudflare Workers & Deno และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Are Edge Secrets?

When building applications, especially at the edge, you often need to handle sensitive information. These are called secrets.

Secrets include things like API keys, database credentials, authentication tokens, and private encryption keys. They are critical for your application's security and functionality.

Dangers of Hardcoding Secrets

A common mistake, especially for beginners, is to hardcode secrets directly into your application's source code.

  • Exposure: Anyone with access to your code (e.g., in a public Git repository) can see your secrets.
  • Rotation Issues: Changing a secret means changing and redeploying your code everywhere it's used.
  • Security Breach: A single leak can compromise your entire system.

Cloudflare Worker Environment Variables

Cloudflare Workers provide a secure way to manage secrets using environment variables. These variables are:

  • Injected securely at runtime, not stored in your code.
  • Encrypted at rest by Cloudflare.
  • Specific to each Worker, allowing fine-grained control.

They are accessed via the env object in your Worker's fetch handler.

Accessing Worker Secrets Demo

This Worker accesses an environment variable named MY_API_KEY. Notice how the secret itself is never hardcoded in the script.

Try running it! (The actual key won't be shown for security).

export default {
  async fetch(request, env, ctx) {
    const apiKey = env.MY_API_KEY; // Access the secret

    if (apiKey) {
      return new Response(`Secret accessed! Length: ${apiKey.length}`);
    } else {
      return new Response(
        "MY_API_KEY environment variable not set.",
        { status: 400 }
      );
    }
  },
};

Deno Environment Variables

Similar to Workers, Deno applications also use environment variables for secrets. You can access them using Deno.env.

For local development, you might use .env files (though not directly supported by Deno, tools like deno task or third-party modules can help). For Deno Deploy, secrets are configured securely through their platform UI or CLI.

Using Deno Secrets Demo

Here's a simple Deno script that retrieves a secret named DB_PASSWORD from its environment variables. Run this example to see it in action.

const dbPassword = Deno.env.get("DB_PASSWORD");

if (dbPassword) {
  console.log(`Database password accessed. Length: ${dbPassword.length}`);
} else {
  console.log("DB_PASSWORD environment variable not set.");
  console.log("To set: env DB_PASSWORD='mysecret' deno run --allow-env main.ts");
}

Managing Secrets with Wrangler CLI

For Cloudflare Workers, the Wrangler CLI is your go-to tool for managing secrets. It encrypts and securely uploads them to Cloudflare's infrastructure.

  • wrangler secret put <NAME>: Prompts for a secret value and uploads it.
  • wrangler secret delete <NAME>: Removes a secret.
  • wrangler secret list: Lists all secrets for your Worker.

This keeps secrets out of your wrangler.toml file and source code.

Principle of Least Privilege

A crucial security principle is the Principle of Least Privilege. This means:

  • Granting only the minimum necessary permissions to access resources.
  • Only giving access to secrets to the specific services or users that absolutely need them.

Avoid giving a Worker access to a secret it doesn't actually use, even if it seems convenient.

Secret Rotation and Auditing

Even with secure storage, secrets can be compromised. Implement these practices:

  • Secret Rotation: Regularly change your API keys and tokens (e.g., every 90 days). This limits the window of opportunity for attackers if a secret is leaked.
  • Auditing: Monitor and log access to your secrets. This helps detect unusual activity and potential breaches.

Automate these processes where possible to reduce manual overhead.

Secrets Management Check

Test your understanding of secure secrets management.

Recap: Secure Secrets

In this lesson, we learned about the importance of securely managing sensitive information in edge applications.

  • Never hardcode secrets.
  • Utilize platform-provided environment variables (Cloudflare Workers env, Deno Deno.env).
  • Use tools like Wrangler CLI for secure secret deployment.
  • Follow the Principle of Least Privilege.
  • Implement regular secret rotation and auditing.

These practices are fundamental to building robust and secure edge applications.

คำถามที่พบบ่อย

บทเรียน “การจัดการข้อมูลลับอย่างปลอดภัย” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการข้อมูลลับอย่างปลอดภัย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อมูลลับอย่างปลอดภัย”

เรียนรู้แนวทางปฏิบัติที่ดีในการจัดการคีย์ API โทเค็น และข้อมูลละเอียดอ่อนอื่น ๆ อย่างปลอดภัยในสภาพแวดล้อมใกล้ผู้ใช้ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการข้อมูลลับอย่างปลอดภัย” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม

ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การยืนยันตัวตนและการอนุญาต
  2. การจำกัดอัตราและการป้องกัน DDoS
  3. การจัดการข้อมูลลับอย่างปลอดภัย
  4. การทำความสะอาดข้อมูลนำเข้าและการป้องกันอินเจกชัน
← กลับไปที่ Edge Computing with Cloudflare Workers & Deno