2013-03-17 17:06:52 -04:00
#
2013-10-27 21:59:46 +01:00
# Copyright 2010, 2012-2013 Red Hat, Inc.
2013-03-17 17:06:52 -04:00
#
2018-04-04 14:35:41 +01:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 15:00:02 -04:00
# See the COPYING file in the top-level directory.
2013-03-17 17:06:52 -04:00
2018-03-20 15:10:04 -04:00
from . . xmlbuilder import XMLBuilder , XMLProperty
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2018-03-20 15:10:04 -04:00
class DomainSeclabel ( XMLBuilder ) :
2013-03-17 17:06:52 -04:00
"""
Class for generating < seclabel > XML
"""
2013-07-16 12:30:43 -04:00
TYPE_DYNAMIC = " dynamic "
TYPE_STATIC = " static "
2013-03-17 17:06:52 -04:00
2013-07-16 12:30:43 -04:00
MODEL_TEST = " testSecurity "
MODEL_SELINUX = " selinux "
MODEL_DAC = " dac "
MODEL_NONE = " none "
2013-03-17 17:06:52 -04:00
2018-03-21 10:53:34 -04:00
XML_NAME = " seclabel "
2019-06-09 17:05:57 -04:00
_XML_PROP_ORDER = [ " type " , " model " , " relabel " , " label " ]
2013-03-17 17:06:52 -04:00
2013-07-16 12:30:43 -04:00
def _guess_secmodel ( self ) :
2018-09-02 09:52:00 -04:00
caps_models = [ x . model for x in self . conn . caps . host . secmodels ]
2013-03-17 17:06:52 -04:00
# We always want the testSecurity model when running tests
2018-09-02 09:52:00 -04:00
if self . MODEL_TEST in caps_models :
2013-07-16 12:30:43 -04:00
return self . MODEL_TEST
2019-06-09 17:05:57 -04:00
if not self . label :
2018-09-02 09:52:00 -04:00
return caps_models and caps_models [ 0 ] or None
2013-03-17 17:06:52 -04:00
2019-06-09 17:05:57 -04:00
lab_len = None
2018-09-02 09:52:00 -04:00
if self . label :
lab_len = min ( 3 , len ( self . label . split ( ' : ' ) ) )
2013-03-17 17:06:52 -04:00
if lab_len == 3 :
2013-07-16 12:30:43 -04:00
return self . MODEL_SELINUX
2013-03-17 17:06:52 -04:00
elif lab_len == 2 :
2013-07-16 12:30:43 -04:00
return self . MODEL_DAC
2018-09-02 09:52:00 -04:00
model = XMLProperty ( " ./@model " )
type = XMLProperty ( " ./@type " )
2013-07-16 12:30:43 -04:00
2013-09-19 13:27:30 -04:00
label = XMLProperty ( " ./label " )
2015-05-03 18:08:10 -04:00
baselabel = XMLProperty ( " ./baselabel " )
2013-09-19 13:27:30 -04:00
relabel = XMLProperty ( " ./@relabel " , is_yesno = True )
2018-09-02 09:52:00 -04:00
##################
# Default config #
##################
def set_defaults ( self , _guest ) :
2022-02-18 09:39:19 -05:00
if not self . type and not self . model :
# Let libvirt fill it in
return
if self . type is None :
self . type = self . TYPE_DYNAMIC
if self . model is None :
self . model = self . _guess_secmodel ( )