29
29
config_client = boto3 .client ("config" , region_name = AWS_REGION )
30
30
31
31
32
- def check_data_sources (kb_id : str , kb_name : str ) -> str | None : # type: ignore # noqa: CFQ004
33
- """Check if a knowledge base's data sources are encrypted.
32
+ def check_data_sources (kb_id : str , kb_name : str ) -> str | None : # type: ignore # noqa: CFQ004, CCR001
33
+ """Check if a knowledge base's data sources are encrypted with KMS during ingestion .
34
34
35
35
Args:
36
36
kb_id (str): Knowledge base ID
@@ -44,18 +44,41 @@ def check_data_sources(kb_id: str, kb_name: str) -> str | None: # type: ignore
44
44
"""
45
45
try :
46
46
data_sources = bedrock_agent_client .list_data_sources (knowledgeBaseId = kb_id )
47
+ LOGGER .info (f"Data sources: { data_sources } " )
47
48
if not isinstance (data_sources , dict ):
48
49
return f"{ kb_name } (invalid data sources response)"
50
+
49
51
unencrypted_sources = []
50
52
for source in data_sources .get ("dataSourceSummaries" , []):
53
+ LOGGER .info (f"Source: { source } " )
51
54
if not isinstance (source , dict ):
52
55
continue
53
- encryption_config = source .get ("serverSideEncryptionConfiguration" , {})
54
- if not isinstance (encryption_config , dict ) or not encryption_config .get ("kmsKeyArn" ):
55
- unencrypted_sources .append (source .get ("name" , source ["dataSourceId" ]))
56
+
57
+ # Get the detailed data source configuration
58
+ try :
59
+ source_details = bedrock_agent_client .get_data_source (
60
+ knowledgeBaseId = kb_id ,
61
+ dataSourceId = source ["dataSourceId" ]
62
+ )
63
+ LOGGER .info (f"Source details: { source_details } " )
64
+
65
+ # Check for KMS encryption configuration
66
+ data_source = source_details .get ("dataSource" , {})
67
+ encryption_config = data_source .get ("serverSideEncryptionConfiguration" , {})
68
+ LOGGER .info (f"Encryption config: { encryption_config } " )
69
+
70
+ # Check if KMS key is configured for encryption
71
+ if not encryption_config .get ("kmsKeyArn" ):
72
+ unencrypted_sources .append (source .get ("name" , source ["dataSourceId" ]))
73
+
74
+ except ClientError as e :
75
+ LOGGER .error (f"Error getting data source details for { source .get ('name' , source ['dataSourceId' ])} : { str (e )} " )
76
+ if e .response ["Error" ]["Code" ] == "AccessDeniedException" :
77
+ unencrypted_sources .append (f"{ source .get ('name' , source ['dataSourceId' ])} (access denied)" )
78
+ continue
56
79
57
80
if unencrypted_sources :
58
- return f"{ kb_name } (unencrypted sources: { ', ' .join (unencrypted_sources )} )"
81
+ return f"{ kb_name } (sources without KMS encryption : { ', ' .join (unencrypted_sources )} )"
59
82
return None
60
83
except ClientError as e :
61
84
LOGGER .error (f"Error checking data sources for knowledge base { kb_name } : { str (e )} " )
0 commit comments