1515# specific language governing permissions and limitations
1616# under the License.
1717
18- import re
1918import time
20- import warnings
2119from itertools import chain
2220from platform import python_version
2321
2422from ._version import __versionstr__
25- from .compat import Lock
2623from .connection import Urllib3HttpConnection
2724from .connection_pool import ConnectionPool , DummyConnectionPool , EmptyConnectionPool
2825from .exceptions import (
29- AuthenticationException ,
30- AuthorizationException ,
3126 ConnectionError ,
3227 ConnectionTimeout ,
3328 ElasticsearchWarning ,
@@ -404,14 +399,6 @@ def perform_request(self, method, url, headers=None, params=None, body=None):
404399 method , headers , params , body
405400 )
406401
407- # Before we make the actual API call we verify the Elasticsearch instance.
408- if self ._verified_elasticsearch is None :
409- self ._do_verify_elasticsearch (headers = headers , timeout = timeout )
410-
411- # If '_verified_elasticsearch' isn't 'True' then we raise an error.
412- if self ._verified_elasticsearch is not True :
413- _ProductChecker .raise_error (self ._verified_elasticsearch )
414-
415402 for attempt in range (self .max_retries + 1 ):
416403 connection = self .get_connection ()
417404
@@ -521,88 +508,6 @@ def _resolve_request_args(self, method, headers, params, body):
521508
522509 return method , headers , params , body , ignore , timeout
523510
524- def _do_verify_elasticsearch (self , headers , timeout ):
525- """Verifies that we're connected to an Elasticsearch cluster.
526- This is done at least once before the first actual API call
527- and makes a single request to the 'GET /' API endpoint to
528- check the version along with other details of the response.
529-
530- If we're unable to verify we're talking to Elasticsearch
531- but we're also unable to rule it out due to a permission
532- error we instead emit an 'ElasticsearchWarning'.
533- """
534- # Ensure that there's only one thread within this section
535- # at a time to not emit unnecessary index API calls.
536- with self ._verify_elasticsearch_lock :
537-
538- # Product check has already been completed while we were
539- # waiting our turn, no need to do again.
540- if self ._verified_elasticsearch is not None :
541- return
542-
543- headers = {
544- header .lower (): value for header , value in (headers or {}).items ()
545- }
546- # We know we definitely want JSON so request it via 'accept'
547- headers .setdefault ("accept" , "application/json" )
548-
549- info_headers = {}
550- info_response = {}
551- error = None
552-
553- attempted_conns = []
554- for conn in chain (self .connection_pool .connections , self .seed_connections ):
555- # Only attempt once per connection max.
556- if conn in attempted_conns :
557- continue
558- attempted_conns .append (conn )
559-
560- try :
561- _ , info_headers , info_response = conn .perform_request (
562- "GET" , "/" , headers = headers , timeout = timeout
563- )
564-
565- # Lowercase all the header names for consistency in accessing them.
566- info_headers = {
567- header .lower (): value for header , value in info_headers .items ()
568- }
569-
570- info_response = self .deserializer .loads (
571- info_response , mimetype = "application/json"
572- )
573- break
574-
575- # Previous versions of 7.x Elasticsearch required a specific
576- # permission so if we receive HTTP 401/403 we should warn
577- # instead of erroring out.
578- except (AuthenticationException , AuthorizationException ):
579- warnings .warn (
580- (
581- "The client is unable to verify that the server is "
582- "Elasticsearch due security privileges on the server side"
583- ),
584- ElasticsearchWarning ,
585- stacklevel = 5 ,
586- )
587- self ._verified_elasticsearch = True
588- return
589-
590- # This connection didn't work, we'll try another.
591- except (ConnectionError , SerializationError , TransportError ) as err :
592- if error is None :
593- error = err
594-
595- # If we received a connection error and weren't successful
596- # anywhere then we re-raise the more appropriate error.
597- if error and not info_response :
598- raise error
599-
600- # Check the information we got back from the index request.
601- self ._verified_elasticsearch = _ProductChecker .check_product (
602- info_headers , info_response
603- )
604-
605-
606511class _ProductChecker :
607512 """Class which verifies we're connected to a supported product"""
608513
@@ -624,17 +529,17 @@ def raise_error(cls, state):
624529 )
625530 else : # UNSUPPORTED_PRODUCT
626531 message = (
627- "The client noticed that the server is not Elasticsearch "
628- "and we do not support this unknown product"
532+ "The client noticed that the server is not Elasticsearch"
629533 )
630534 raise UnsupportedProductError (message )
631535
632536 @classmethod
633537 def check_product (cls , headers , response ):
634538 # type: (dict[str, str], dict[str, str]) -> int
635- """Verifies that the server we're talking to is Elasticsearch.
539+ """Checks whether the server we're talking to is Elasticsearch.
636540 Does this by checking HTTP headers and the deserialized
637- response to the 'info' API. Returns one of the states above.
541+ response to the 'info' API. Returns true - all servers are
542+ acceptable.
638543 """
639544 try :
640545 version = response .get ("version" , {})
@@ -661,21 +566,4 @@ def check_product(cls, headers, response):
661566 bad_build_flavor = True
662567 bad_product_header = True
663568
664- # 7.0-7.13 and there's a bad 'tagline' or unsupported 'build_flavor'
665- if (7 , 0 , 0 ) <= version_number < (7 , 14 , 0 ):
666- if bad_tagline :
667- return cls .UNSUPPORTED_PRODUCT
668- elif bad_build_flavor :
669- return cls .UNSUPPORTED_DISTRIBUTION
670-
671- elif (
672- # No version or version less than 6.x
673- version_number < (6 , 0 , 0 )
674- # 6.x and there's a bad 'tagline'
675- or ((6 , 0 , 0 ) <= version_number < (7 , 0 , 0 ) and bad_tagline )
676- # 7.14+ and there's a bad 'X-Elastic-Product' HTTP header
677- or ((7 , 14 , 0 ) <= version_number and bad_product_header )
678- ):
679- return cls .UNSUPPORTED_PRODUCT
680-
681569 return True
0 commit comments