7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/rails-mcp-server/tools/analyze_environment_config.rb', line 7
def call
unless current_project
message = "No active project. Please switch to a project first."
log(:warn, message)
return message
end
env_dir = File.join(active_project_path, "config", "environments")
unless File.directory?(env_dir)
message = "Environment configuration directory not found at config/environments."
log(:warn, message)
return message
end
env_files = {}
env_settings = {}
Dir.glob(File.join(env_dir, "*.rb")).each do |file|
env_name = File.basename(file, ".rb")
env_files[env_name] = file
env_content = File.read(file)
env_settings[env_name] = (env_content)
end
env_vars_in_code = find_env_vars_in_codebase(active_project_path)
dotenv_files = {}
dotenv_vars = {}
dotenv_patterns = [
".env",
".env.development",
".env.test",
".env.production",
".env.local",
".env.development.local",
".env.test.local",
".env.production.local"
]
dotenv_patterns.each do |pattern|
file_path = File.join(active_project_path, pattern)
if File.exist?(file_path)
dotenv_files[pattern] = file_path
dotenv_vars[pattern] = parse_dotenv_file(file_path)
end
end
credentials_files = {}
credentials_key_file = File.join(active_project_path, "config", "master.key")
credentials_file = File.join(active_project_path, "config", "credentials.yml.enc")
if File.exist?(credentials_file)
credentials_files["credentials.yml.enc"] = credentials_file
end
Dir.glob(File.join(active_project_path, "config", "credentials", "*.yml.enc")).each do |file|
env_name = File.basename(file, ".yml.enc")
credentials_files["credentials/#{env_name}.yml.enc"] = file
end
database_config_file = File.join(active_project_path, "config", "database.yml")
database_config = {}
if File.exist?(database_config_file)
database_config = parse_database_config(database_config_file)
end
env_diff = compare_environment_settings(env_settings)
missing_env_vars = find_missing_env_vars(env_vars_in_code, dotenv_vars)
security_findings = check_security_configuration(env_settings, database_config)
output = []
output << "Environment Configuration Analysis"
output << "=================================="
output << ""
output << "Environment Files:"
env_files.each do |env, file|
output << " - #{env}: #{file.sub("#{active_project_path}/", "")}"
end
output << ""
output << "Environment Variables Usage:"
output << " Total unique ENV variables found in codebase: #{env_vars_in_code.keys.size}"
output << ""
if missing_env_vars.any?
output << "Missing ENV Variables:"
missing_env_vars.each do |env_var, environments|
output << " - #{env_var}: Used in codebase but missing in #{environments.join(", ")}"
end
else
output << "All ENV variables appear to be defined in at least one .env file."
end
output << ""
if env_diff[:unique_settings].any?
output << "Environment-Specific Settings:"
env_diff[:unique_settings].each do |env, settings|
output << " #{env}:"
settings.each do |setting|
output << " - #{setting}"
end
end
output << ""
end
if env_diff[:different_values].any?
output << "Settings with Different Values Across Environments:"
env_diff[:different_values].each do |setting, values|
output << " #{setting}:"
values.each do |env, value|
output << " - #{env}: #{value}"
end
end
output << ""
end
output << "Credentials Management:"
if credentials_files.any?
output << " Encrypted credentials files found:"
credentials_files.each do |name, file|
output << " - #{name}"
end
output << if File.exist?(credentials_key_file)
" Master key file exists (config/master.key)"
else
" Warning: No master.key file found. Credentials are likely managed through RAILS_MASTER_KEY environment variable."
end
else
output << " No encrypted credentials files found. The application may be using ENV variables exclusively."
end
output << ""
output << "Database Configuration:"
if database_config.any?
database_config.each do |env, config|
output << " #{env}:"
if config["adapter"]
output << " - Adapter: #{config["adapter"]}"
end
if config["host"] && config["host"] != "localhost" && config["host"] != "127.0.0.1"
output << " - Host: #{config["host"]}"
end
if config["database"]
output << " - Database: #{config["database"]}"
end
if config["username"] && !config["username"].include?("ENV")
output << " - Warning: Database username hardcoded in database.yml"
end
if config["password"] && !config["password"].include?("ENV")
output << " - Warning: Database password hardcoded in database.yml"
end
end
else
output << " Could not parse database configuration."
end
output << ""
if security_findings.any?
output << "Security Configuration Findings:"
security_findings.each do |finding|
output << " - #{finding}"
end
output << ""
end
output.join("\n")
end
|