Repository: ansible-collections/community.general

Issue:
java_cert: Add parameter for certificate data 
### Summary

I need to ensure that a certain certificate is present in a Java truststore. Currently, I have to upload the certificate file first in order to use the `java_cert` module. Despite the extra step necessary, it makes the check mode less useful since it will fail when the certificate file is not present already.

The `java_keystore` module accepts a [`certificate`-parameter](https://docs.ansible.com/ansible/latest/collections/community/general/java_keystore_module.html#parameter-certificate) which allows to provide the certificate data directly. In the same manner, a `cert_data` parameter would be useful.

### Issue Type

Feature Idea

### Component Name

java_cert

### Additional Information

```yaml
- community.general.java_cert:
    cert_alias: example
    cert_data: "{{ lookup('file', '/path/to/certificate.crt') }}"
    keystore_path: "{{ truststore_path }}"
    keystore_password: "{{ truststore_password }}"
```

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/java_cert.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/java_cert.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @absynth76 @haad
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/java_cert.py b/plugins/modules/java_cert.py
--- a/plugins/modules/java_cert.py
+++ b/plugins/modules/java_cert.py
@@ -28,7 +28,7 @@
   cert_url:
     description:
       - Basic URL to fetch SSL certificate from.
-      - Exactly one of O(cert_url), O(cert_path), or O(pkcs12_path) is required to load certificate.
+      - Exactly one of O(cert_url), O(cert_path), O(cert_content), or O(pkcs12_path) is required to load certificate.
     type: str
   cert_port:
     description:
@@ -39,8 +39,14 @@
   cert_path:
     description:
       - Local path to load certificate from.
-      - Exactly one of O(cert_url), O(cert_path), or O(pkcs12_path) is required to load certificate.
+      - Exactly one of O(cert_url), O(cert_path), O(cert_content), or O(pkcs12_path) is required to load certificate.
     type: path
+  cert_content:
+    description:
+      - Content of the certificate used to create the keystore.
+      - Exactly one of O(cert_url), O(cert_path), O(cert_content), or O(pkcs12_path) is required to load certificate.
+    type: str
+    version_added: 8.6.0
   cert_alias:
     description:
       - Imported certificate alias.
@@ -55,10 +61,10 @@
   pkcs12_path:
     description:
       - Local path to load PKCS12 keystore from.
-      - Unlike O(cert_url) and O(cert_path), the PKCS12 keystore embeds the private key matching
+      - Unlike O(cert_url), O(cert_path) and O(cert_content), the PKCS12 keystore embeds the private key matching
         the certificate, and is used to import both the certificate and its private key into the
         java keystore.
-      - Exactly one of O(cert_url), O(cert_path), or O(pkcs12_path) is required to load certificate.
+      - Exactly one of O(cert_url), O(cert_path), O(cert_content), or O(pkcs12_path) is required to load certificate.
     type: path
   pkcs12_password:
     description:
@@ -149,6 +155,19 @@
     cert_alias: LE_RootCA
     trust_cacert: true
 
+- name: Import trusted CA from the SSL certificate stored in the cert_content variable
+  community.general.java_cert:
+    cert_content: |
+      -----BEGIN CERTIFICATE-----
+      ...
+      -----END CERTIFICATE-----
+    keystore_path: /tmp/cacerts
+    keystore_pass: changeit
+    keystore_create: true
+    state: present
+    cert_alias: LE_RootCA
+    trust_cacert: true
+
 - name: Import SSL certificate from google.com to a keystore, create it if it doesn't exist
   community.general.java_cert:
     cert_url: google.com
@@ -487,6 +506,7 @@ def main():
     argument_spec = dict(
         cert_url=dict(type='str'),
         cert_path=dict(type='path'),
+        cert_content=dict(type='str'),
         pkcs12_path=dict(type='path'),
         pkcs12_password=dict(type='str', no_log=True),
         pkcs12_alias=dict(type='str'),
@@ -503,11 +523,11 @@ def main():
 
     module = AnsibleModule(
         argument_spec=argument_spec,
-        required_if=[['state', 'present', ('cert_path', 'cert_url', 'pkcs12_path'), True],
+        required_if=[['state', 'present', ('cert_path', 'cert_url', 'cert_content', 'pkcs12_path'), True],
                      ['state', 'absent', ('cert_url', 'cert_alias'), True]],
         required_together=[['keystore_path', 'keystore_pass']],
         mutually_exclusive=[
-            ['cert_url', 'cert_path', 'pkcs12_path']
+            ['cert_url', 'cert_path', 'cert_content', 'pkcs12_path']
         ],
         supports_check_mode=True,
         add_file_common_args=True,
@@ -515,6 +535,7 @@ def main():
 
     url = module.params.get('cert_url')
     path = module.params.get('cert_path')
+    content = module.params.get('cert_content')
     port = module.params.get('cert_port')
 
     pkcs12_path = module.params.get('pkcs12_path')
@@ -582,6 +603,10 @@ def main():
             # certificate to stdout so we don't need to do any transformations.
             new_certificate = path
 
+        elif content:
+            with open(new_certificate, "w") as f:
+                f.write(content)
+
         elif url:
             # Getting the X509 digest from a URL is the same as from a path, we just have
             # to download the cert first


Test patch: