Skip to content

Commit

Permalink
Merge pull request #367 from IMIO/DEVOPS-3
Browse files Browse the repository at this point in the history
feat: add support of https connections
  • Loading branch information
davisagli committed Feb 13, 2024
2 parents 5e6c953 + 945f61e commit 8f72739
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
9.2.4 (unreleased)
------------------

- Add support of https connections
[remdub]

- Add french locales
[remdub]

Expand Down
24 changes: 14 additions & 10 deletions docs/usage/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Example::
COLLECTIVE_SOLR_BASE /solr/plone
COLLECTIVE_SOLR_LOGIN solr_login
COLLECTIVE_SOLR_PASSWORD solr_password
COLLECTIVE_SOLR_HTTPS_CONNECTION true
COLLECTIVE_SOLR_IGNORE_CERTIFICATE_CHECK false

Configuration priority
......................
Expand All @@ -43,16 +45,18 @@ Since parameters are limited for ZCML and Environement Variables methods, the th

Here is a list of parameters and methods

==================== =============== ======== ========================= =======
**Parameter** **Example** **ZCML** **Environment Variables** **TTW**
==================== =============== ======== ========================= =======
**Host** localhost YES YES YES
**Port** 8983 YES YES YES
**Base** /solr/plone YES YES YES
**Login** login NO YES YES
**Password** password NO YES YES
**Other parameters** NO NO YES
==================== =============== ======== ========================= =======
============================ =============== ======== ========================= =======
**Parameter** **Example** **ZCML** **Environment Variables** **TTW**
============================ =============== ======== ========================= =======
**Host** localhost YES YES YES
**Port** 8983 YES YES YES
**Base** /solr/plone YES YES YES
**Login** login NO YES YES
**Password** password NO YES YES
**Https connection** true NO YES YES
**Ignore certificate check** false NO YES YES
**Other parameters** NO NO YES
============================ =============== ======== ========================= =======


TTW Configuration
Expand Down
18 changes: 18 additions & 0 deletions src/collective/solr/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ class ISolrSchema(Interface):
required=False,
)

https_connection = Bool(
title=_(
"label_https_connection",
default="Use HTTPS connection",
),
default=False,
required=False,
)

ignore_certificate_check = Bool(
title=_(
"label_ignore_certificate_check",
default="Ignore certificate check",
),
default=False,
required=False,
)

force_simple_search = Bool(
title=_("label_force_simple_search", default="Force simple search pattern"),
description=_(
Expand Down
18 changes: 18 additions & 0 deletions src/collective/solr/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def none_formatter(value):
return value


def bool_formatter(value):
if isinstance(value, bool):
return value
return value.lower() in ("true", "1")


@implementer(IZCMLSolrConnectionConfig)
class ZCMLSolrConnectionConfig(object):
"""Connection values that can be configured through zcml"""
Expand Down Expand Up @@ -109,6 +115,14 @@ def getConnection(self):
env_key="COLLECTIVE_SOLR_PASSWORD",
formatter=none_formatter,
)
config_https_connection = self.getConfigParameter(
"collective.solr.https_connection",
formatter=bool_formatter,
)
config_ignore_certificate_check = self.getConfigParameter(
"collective.solr.ignore_certificate_check",
formatter=bool_formatter,
)
if zcmlconfig is not None:
# use connection parameters defined in zcml...
logger.debug("opening connection to %s", zcmlconfig.host)
Expand All @@ -118,6 +132,8 @@ def getConnection(self):
persistent=True,
login=config_login,
password=config_password,
https_connection=config_https_connection,
ignore_certificate_check=config_ignore_certificate_check,
manager=self,
)
setLocal(self.connection_key, conn)
Expand All @@ -133,6 +149,8 @@ def getConnection(self):
persistent=True,
login=config_login,
password=config_password,
https_connection=config_https_connection,
ignore_certificate_check=config_ignore_certificate_check,
manager=self,
)
setLocal(self.connection_key, conn)
Expand Down
2 changes: 1 addition & 1 deletion src/collective/solr/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<metadata>
<version>8</version>
<version>9</version>
<dependencies>
<dependency>profile-plone.app.registry:default</dependency>
<dependency>profile-plone.restapi:default</dependency>
Expand Down
2 changes: 2 additions & 0 deletions src/collective/solr/profiles/default/registry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<value key="prefix_wildcard">False</value>
<value key="force_simple_search">False</value>
<value key="allow_complex_search">False</value>
<value key="https_connection">False</value>
<value key="ignore_certificate_check">False</value>
<value key="required">
<element>SearchableText</element>
</value>
Expand Down
15 changes: 15 additions & 0 deletions src/collective/solr/setuphandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,18 @@ def migrate_to_8(context):
registry_record.value = ""
registry.records["collective.solr.stopwords"] = registry_record
logger.info("Migrated to version 8")


def migrate_to_9(context):
registry = getUtility(IRegistry)
if "collective.solr.https_connection" not in registry.records:
registry_field = field.Bool(title=u"Use HTTPS connection")
registry_record = Record(registry_field)
registry_record.value = False
registry.records["collective.solr.https_connection"] = registry_record
if "collective.solr.ignore_certificate_check" not in registry.records:
registry_field = field.Bool(title=u"Ignore certificate check")
registry_record = Record(registry_field)
registry_record.value = False
registry.records["collective.solr.ignore_certificate_check"] = registry_record
logger.info("Migrated to version 9")
17 changes: 16 additions & 1 deletion src/collective/solr/solr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import base64
import codecs
import socket
import ssl
from copy import deepcopy
from logging import getLogger
from xml.etree.cElementTree import fromstring
Expand Down Expand Up @@ -62,6 +63,8 @@ def __init__(
timeout=None,
login=None,
password=None,
https_connection=False,
ignore_certificate_check=False,
manager=None,
):
self.manager = manager
Expand All @@ -73,7 +76,19 @@ def __init__(
# responses from Solr will always be in UTF-8
self.decoder = codecs.getdecoder("utf-8")
# a real connection to the server is not opened at this point.
self.conn = six.moves.http_client.HTTPConnection(self.host, timeout=timeout)
self.https_connection = https_connection
self.ignore_certificate_check = ignore_certificate_check
if self.https_connection:
if self.ignore_certificate_check:
self.conn = six.moves.http_client.HTTPSConnection(
self.host, timeout=timeout, context=ssl._create_unverified_context()
)
else:
self.conn = six.moves.http_client.HTTPSConnection(
self.host, timeout=timeout
)
else:
self.conn = six.moves.http_client.HTTPConnection(self.host, timeout=timeout)
# self.conn.set_debuglevel(1000000)
self.login = login
self.auth_headers = {}
Expand Down
2 changes: 2 additions & 0 deletions src/collective/solr/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ def __init__(self):
self.boost_script = ""
self.solr_login = None
self.solr_password = None
self.https_connection = False
self.ignore_certificate_check = False
self.stopwords = ""
self.stopwords_case_insensitive = False

Expand Down
12 changes: 12 additions & 0 deletions src/collective/solr/tests/test_solr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from collective.solr.tests.utils import fakehttp, getData
from collective.solr.utils import getConfig

import six.moves.http_client


class TestSolr(TestCase):
layer = COLLECTIVE_SOLR_MOCK_REGISTRY_FIXTURE
Expand Down Expand Up @@ -261,3 +263,13 @@ def test_delete(self):
self.failUnlessEqual(node.attrib["name"], "QTime")
self.failUnlessEqual(node.text, "0")
res.find("QTime")

def test_conn(self):
# http connection
c = SolrConnection(host="localhost:8983", persistent=True)
self.assertTrue(type(c.conn) is six.moves.http_client.HTTPConnection)
# https connection
c = SolrConnection(
host="localhost:8983", persistent=True, https_connection=True
)
self.assertTrue(type(c.conn) is six.moves.http_client.HTTPSConnection)
10 changes: 10 additions & 0 deletions src/collective/solr/upgrades.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@
profile="collective.solr:default"
/>

<genericsetup:upgradeStep
title="Upgrade to collective.solr 9"
description="Add https_connection and ignore_certificate_check registry entries"
source="8"
destination="9"
handler=".setuphandlers.migrate_to_9"
sortkey="1"
profile="collective.solr:default"
/>

</configure>

0 comments on commit 8f72739

Please sign in to comment.