Spaces:
Running
Running
Commit
Β·
f89b28b
1
Parent(s):
c110c9c
SECURITY: Remove .cdsapirc file and implement environment variable authentication
Browse files- Remove sensitive .cdsapirc file from repository
- Update cams_downloader.py to prioritize environment variables
- Add .cdsapirc to .gitignore to prevent future commits
- Update error messages and documentation
- Add instructions for secure credential setup in Hugging Face Spaces
- .cdsapirc +0 -2
- .gitignore +5 -0
- README.md +20 -2
- app.py +1 -1
- cams_downloader.py +18 -9
- templates/index.html +3 -3
.cdsapirc
DELETED
|
@@ -1,2 +0,0 @@
|
|
| 1 |
-
url: https://ads.atmosphere.copernicus.eu/api
|
| 2 |
-
key: 4492fecf-e164-45ed-9d8e-fc86ca282600
|
|
|
|
|
|
|
|
|
.gitignore
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
.DS_Store
|
| 2 |
__pycache__/
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Any file in these folders should be ignored.
|
| 5 |
static/
|
| 6 |
plots/
|
|
|
|
| 1 |
.DS_Store
|
| 2 |
__pycache__/
|
| 3 |
|
| 4 |
+
# Credentials and sensitive files
|
| 5 |
+
.cdsapirc
|
| 6 |
+
*.env
|
| 7 |
+
.env*
|
| 8 |
+
|
| 9 |
# Any file in these folders should be ignored.
|
| 10 |
static/
|
| 11 |
plots/
|
README.md
CHANGED
|
@@ -33,6 +33,24 @@ A comprehensive web application for visualizing atmospheric composition data fro
|
|
| 33 |
|
| 34 |
This application uses data from the Copernicus Atmosphere Monitoring Service (CAMS), which provides global atmospheric composition forecasts and analyses.
|
| 35 |
|
| 36 |
-
##
|
| 37 |
|
| 38 |
-
For downloading CAMS data, you
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
This application uses data from the Copernicus Atmosphere Monitoring Service (CAMS), which provides global atmospheric composition forecasts and analyses.
|
| 35 |
|
| 36 |
+
## CDS API Configuration
|
| 37 |
|
| 38 |
+
For downloading CAMS data, you need to set up CDS API credentials:
|
| 39 |
+
|
| 40 |
+
### For Hugging Face Spaces (Recommended):
|
| 41 |
+
1. Create an account at https://cds.climate.copernicus.eu/
|
| 42 |
+
2. Go to your user profile and copy your API credentials
|
| 43 |
+
3. In your Hugging Face Space settings, add these as **Secrets**:
|
| 44 |
+
- `CDSAPI_URL`: `https://ads.atmosphere.copernicus.eu/api`
|
| 45 |
+
- `CDSAPI_KEY`: Your API key from the CDS website
|
| 46 |
+
|
| 47 |
+
### For Local Development:
|
| 48 |
+
You can create a `.cdsapirc` file in your home directory:
|
| 49 |
+
```
|
| 50 |
+
url: https://ads.atmosphere.copernicus.eu/api
|
| 51 |
+
key: your-api-key-here
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
**Note**: Never commit `.cdsapirc` files to public repositories as they contain sensitive credentials.
|
| 55 |
+
|
| 56 |
+
You can also upload your own NetCDF files to explore the visualization features without API access.
|
app.py
CHANGED
|
@@ -139,7 +139,7 @@ def download_date():
|
|
| 139 |
# --- End of Validation Logic ---
|
| 140 |
|
| 141 |
if not downloader.is_client_ready():
|
| 142 |
-
flash('CDS API not configured. Please check your .cdsapirc file.', 'error')
|
| 143 |
return redirect(url_for('index'))
|
| 144 |
|
| 145 |
try:
|
|
|
|
| 139 |
# --- End of Validation Logic ---
|
| 140 |
|
| 141 |
if not downloader.is_client_ready():
|
| 142 |
+
flash('CDS API not configured. Please check your environment variables or .cdsapirc file.', 'error')
|
| 143 |
return redirect(url_for('index'))
|
| 144 |
|
| 145 |
try:
|
cams_downloader.py
CHANGED
|
@@ -29,7 +29,16 @@ class CAMSDownloader:
|
|
| 29 |
def _init_client(self):
|
| 30 |
"""Initialize CDS API client"""
|
| 31 |
try:
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
cdsapirc_path = Path.cwd() / ".cdsapirc"
|
| 34 |
if not cdsapirc_path.exists():
|
| 35 |
cdsapirc_path = Path.home() / ".cdsapirc"
|
|
@@ -48,23 +57,23 @@ class CAMSDownloader:
|
|
| 48 |
elif line.startswith('key:'):
|
| 49 |
key = line.split(':', 1)[1].strip()
|
| 50 |
|
| 51 |
-
print(url, key)
|
| 52 |
if url and key:
|
| 53 |
self.client = cdsapi.Client(key=key, url=url)
|
| 54 |
-
print("β
CDS API client initialized from .cdsapirc")
|
|
|
|
| 55 |
else:
|
| 56 |
raise ValueError("Could not parse URL or key from .cdsapirc file")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
print(f"β οΈ Warning: Could not initialize CDS API client: {str(e)}")
|
| 64 |
print("Please ensure you have:")
|
| 65 |
print("1. Created an account at https://cds.climate.copernicus.eu/")
|
| 66 |
-
print("2.
|
| 67 |
-
print("3. Or
|
| 68 |
self.client = None
|
| 69 |
|
| 70 |
def is_client_ready(self):
|
|
|
|
| 29 |
def _init_client(self):
|
| 30 |
"""Initialize CDS API client"""
|
| 31 |
try:
|
| 32 |
+
# First, try environment variables (preferred for cloud deployments)
|
| 33 |
+
cdsapi_url = os.getenv('CDSAPI_URL')
|
| 34 |
+
cdsapi_key = os.getenv('CDSAPI_KEY')
|
| 35 |
+
|
| 36 |
+
if cdsapi_url and cdsapi_key:
|
| 37 |
+
self.client = cdsapi.Client(key=cdsapi_key, url=cdsapi_url)
|
| 38 |
+
print("β
CDS API client initialized from environment variables")
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
# Fallback: Try to read .cdsapirc file from current directory first, then home directory
|
| 42 |
cdsapirc_path = Path.cwd() / ".cdsapirc"
|
| 43 |
if not cdsapirc_path.exists():
|
| 44 |
cdsapirc_path = Path.home() / ".cdsapirc"
|
|
|
|
| 57 |
elif line.startswith('key:'):
|
| 58 |
key = line.split(':', 1)[1].strip()
|
| 59 |
|
|
|
|
| 60 |
if url and key:
|
| 61 |
self.client = cdsapi.Client(key=key, url=url)
|
| 62 |
+
print("β
CDS API client initialized from .cdsapirc file")
|
| 63 |
+
return
|
| 64 |
else:
|
| 65 |
raise ValueError("Could not parse URL or key from .cdsapirc file")
|
| 66 |
+
|
| 67 |
+
# Last resort: Try default initialization
|
| 68 |
+
self.client = cdsapi.Client()
|
| 69 |
+
print("β
CDS API client initialized with default settings")
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
print(f"β οΈ Warning: Could not initialize CDS API client: {str(e)}")
|
| 73 |
print("Please ensure you have:")
|
| 74 |
print("1. Created an account at https://cds.climate.copernicus.eu/")
|
| 75 |
+
print("2. Set CDSAPI_URL and CDSAPI_KEY environment variables (recommended for cloud deployments)")
|
| 76 |
+
print("3. Or created a .cdsapirc file in your home directory with your credentials")
|
| 77 |
self.client = None
|
| 78 |
|
| 79 |
def is_client_ready(self):
|
templates/index.html
CHANGED
|
@@ -147,9 +147,9 @@
|
|
| 147 |
{% if cds_ready %}β
CDS API Ready{% else %}β CDS API Not Configured{% endif %}
|
| 148 |
</span>
|
| 149 |
{% if not cds_ready %}
|
| 150 |
-
|
| 151 |
-
Please
|
| 152 |
-
</
|
| 153 |
{% endif %}
|
| 154 |
</div>
|
| 155 |
|
|
|
|
| 147 |
{% if cds_ready %}β
CDS API Ready{% else %}β CDS API Not Configured{% endif %}
|
| 148 |
</span>
|
| 149 |
{% if not cds_ready %}
|
| 150 |
+
<small class="help-text">
|
| 151 |
+
Please configure CDS API credentials via environment variables (CDSAPI_URL and CDSAPI_KEY) to enable data download.
|
| 152 |
+
</small>
|
| 153 |
{% endif %}
|
| 154 |
</div>
|
| 155 |
|