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
- peft
|
| 10 |
+
- aws
|
| 11 |
+
- lora
|
| 12 |
+
- security
|
| 13 |
+
- iam
|
| 14 |
+
- fine-tuned
|
| 15 |
+
---
|
| 16 |
+
Model Card: secure-policy-rewriter 🔐
|
| 17 |
+
This is a fine-tuned version of the facebook/opt-1.3b model designed to analyze and rewrite risky AWS IAM policies into more secure, specific policies based on the Principle of Least Privilege.
|
| 18 |
+
|
| 19 |
+
The model takes a potentially risky IAM policy (often with wildcards like "*" or "s3:*") as input and generates a secure version with specific permissions, resources, and optional conditions. This model can be used as a component in a larger security scanning or remediation pipeline.
|
| 20 |
+
|
| 21 |
+
Model Details
|
| 22 |
+
Base Model: facebook/opt-1.3b
|
| 23 |
+
|
| 24 |
+
Task: Text Generation (text-generation)
|
| 25 |
+
|
| 26 |
+
Fine-tuning Method: Parameter-Efficient Fine-Tuning (PEFT) using LoRA (Low-Rank Adaptation)
|
| 27 |
+
|
| 28 |
+
Libraries: The model was trained using transformers, datasets, peft, and accelerate
|
| 29 |
+
|
| 30 |
+
Training Data
|
| 31 |
+
The model was fine-tuned on a custom dataset of 100000 JSON pairs, with the input being a risky policy and the output being a securely rewritten version. The dataset was generated programmatically within a Kaggle Notebook and contained a mix of policies with specific actions and wildcards for both actions and resources.
|
| 32 |
+
|
| 33 |
+
How to Use 💻
|
| 34 |
+
To use this model, you must first load the base model, then attach the PEFT adapter weights.
|
| 35 |
+
|
| 36 |
+
Dependencies:
|
| 37 |
+
Install the necessary libraries:
|
| 38 |
+
|
| 39 |
+
Bash
|
| 40 |
+
|
| 41 |
+
pip install transformers torch peft accelerate
|
| 42 |
+
Inference Code:
|
| 43 |
+
The following Python script demonstrates how to load the model and perform a rewrite on a sample policy.
|
| 44 |
+
|
| 45 |
+
Python
|
| 46 |
+
|
| 47 |
+
import json
|
| 48 |
+
import torch
|
| 49 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 50 |
+
from peft import PeftModel, LoraConfig, TaskType
|
| 51 |
+
|
| 52 |
+
# Set device
|
| 53 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 54 |
+
|
| 55 |
+
# Load base model and tokenizer
|
| 56 |
+
base_model_id = "facebook/opt-1.3b"
|
| 57 |
+
checkpoint_path = "Yehia3A/secure-policy-rewriter"
|
| 58 |
+
|
| 59 |
+
print("Loading model...")
|
| 60 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
|
| 61 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 62 |
+
base_model_id,
|
| 63 |
+
torch_dtype=torch.float16,
|
| 64 |
+
trust_remote_code=True
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Load LoRA adapter
|
| 68 |
+
lora_config = LoraConfig(
|
| 69 |
+
task_type=TaskType.CAUSAL_LM,
|
| 70 |
+
r=16,
|
| 71 |
+
lora_alpha=32,
|
| 72 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 73 |
+
inference_mode=True
|
| 74 |
+
)
|
| 75 |
+
model = PeftModel.from_pretrained(model, checkpoint_path, config=lora_config)
|
| 76 |
+
model.eval()
|
| 77 |
+
|
| 78 |
+
# Create a text generation pipeline
|
| 79 |
+
llm = pipeline(
|
| 80 |
+
"text-generation",
|
| 81 |
+
model=model,
|
| 82 |
+
tokenizer=tokenizer,
|
| 83 |
+
device=device,
|
| 84 |
+
pad_token_id=tokenizer.eos_token_id
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
def rewrite_policy(policy_json: dict) -> str:
|
| 88 |
+
# Use a few-shot prompt to guide the model
|
| 89 |
+
prompt = f"""Rewrite risky IAM policies to be secure. Replace wildcards with specific permissions and add conditions.
|
| 90 |
+
|
| 91 |
+
Input: {json.dumps(policy_json, indent=2)}
|
| 92 |
+
|
| 93 |
+
Output:"""
|
| 94 |
+
|
| 95 |
+
result = llm(prompt, max_new_tokens=300, temperature=0.05, do_sample=True, pad_token_id=tokenizer.eos_token_id)
|
| 96 |
+
result_text = result[0]["generated_text"].strip()
|
| 97 |
+
|
| 98 |
+
# Simple JSON extraction from the output
|
| 99 |
+
try:
|
| 100 |
+
start_index = result_text.find("{")
|
| 101 |
+
end_index = result_text.rfind("}") + 1
|
| 102 |
+
if start_index != -1 and end_index != -1:
|
| 103 |
+
json_str = result_text[start_index:end_index]
|
| 104 |
+
return json.dumps(json.loads(json_str), indent=2)
|
| 105 |
+
except:
|
| 106 |
+
return "{}"
|
| 107 |
+
return "{}"
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Example risky policy
|
| 111 |
+
risky_policy = {
|
| 112 |
+
"Version": "2012-10-17",
|
| 113 |
+
"Statement": [{
|
| 114 |
+
"Effect": "Allow",
|
| 115 |
+
"Action": "*",
|
| 116 |
+
"Resource": "*"
|
| 117 |
+
}]
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
# Rewrite the policy
|
| 121 |
+
rewritten_policy = rewrite_policy(risky_policy)
|
| 122 |
+
print("Rewritten Secure Policy:")
|
| 123 |
+
print(rewritten_policy)
|
| 124 |
+
|
| 125 |
+
Files
|
| 126 |
+
This repository contains the necessary files to run the fine-tuned model:
|
| 127 |
+
|
| 128 |
+
adapter_config.json: Configuration for the PEFT adapter.
|
| 129 |
+
|
| 130 |
+
adapter_model.safetensors: The fine-tuned weights of the PEFT adapter.
|
| 131 |
+
|
| 132 |
+
tokenizer_config.json: The tokenizer configuration.
|
| 133 |
+
|
| 134 |
+
tokenizer.json: The tokenizer model file.
|
| 135 |
+
|
| 136 |
+
special_tokens_map.json: A mapping of special tokens.
|
| 137 |
+
|
| 138 |
+
merges.txt: A vocabulary file for the tokenizer.
|
| 139 |
+
|
| 140 |
+
Disclaimers
|
| 141 |
+
This model is a proof-of-concept for educational and research purposes. It is not intended for use in production environments without further validation, testing, and security checks. It was trained on a small, synthetic dataset and may not generalize well to all real-world scenarios.
|