Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

# Copyright (C) 2011  Internet Systems Consortium. 

# 

# Permission to use, copy, modify, and distribute this software for any 

# purpose with or without fee is hereby granted, provided that the above 

# copyright notice and this permission notice appear in all copies. 

# 

# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM 

# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 

# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 

# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, 

# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 

# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 

# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 

# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

 

""" 

This helps the XFR in process with accumulating parts of diff and applying 

it to the datasource. 

 

The name of the module is not yet fully decided. We might want to move it 

under isc.datasrc or somewhere else, because we might want to reuse it with 

future DDNS process. But until then, it lives here. 

""" 

 

import isc.dns 

import isc.log 

from isc.log_messages.libxfrin_messages import * 

 

class NoSuchZone(Exception): 

    """ 

    This is raised if a diff for non-existant zone is being created. 

    """ 

    pass 

 

""" 

This is the amount of changes we accumulate before calling Diff.apply 

automatically. 

 

The number 100 is just taken from BIND 9. We don't know the rationale 

for exactly this amount, but we think it is just some randomly chosen 

number. 

""" 

# If changing this, modify the tests accordingly as well. 

DIFF_APPLY_TRESHOLD = 100 

 

logger = isc.log.Logger('libxfrin') 

 

class Diff: 

    """ 

    The class represents a diff against current state of datasource on 

    one zone. The usual way of working with it is creating it, then putting 

    bunch of changes in and commiting at the end. 

 

    If you change your mind, you can just stop using the object without 

    really commiting it. In that case no changes will happen in the data 

    sounce. 

 

    The class works as a kind of a buffer as well, it does not direct 

    the changes to underlying data source right away, but keeps them for 

    a while. 

    """ 

    def __init__(self, ds_client, zone, replace=False, journaling=False): 

        """ 

        Initializes the diff to a ready state. It checks the zone exists 

        in the datasource and if not, NoSuchZone is raised. This also creates 

        a transaction in the data source. 

 

        The ds_client is the datasource client containing the zone. Zone is 

        isc.dns.Name object representing the name of the zone (its apex). 

        If replace is True, the content of the whole zone is wiped out before 

        applying the diff. 

 

        If journaling is True, the history of subsequent updates will be 

        recorded as well as the updates themselves as long as the underlying 

        data source support the journaling.  If the data source allows 

        incoming updates but does not support journaling, the Diff object 

        will still continue applying the diffs with disabling journaling. 

 

        You can also expect isc.datasrc.Error or isc.datasrc.NotImplemented 

        exceptions. 

        """ 

        try: 

            self.__updater = ds_client.get_updater(zone, replace, journaling) 

        except isc.datasrc.NotImplemented as ex: 

86            if not journaling: 

                raise ex 

            self.__updater = ds_client.get_updater(zone, replace, False) 

            logger.info(LIBXFRIN_NO_JOURNAL, zone, ds_client) 

        if self.__updater is None: 

            # The no such zone case 

            raise NoSuchZone("Zone " + str(zone) + 

                             " does not exist in the data source " + 

                             str(ds_client)) 

        self.__buffer = [] 

 

    def __check_commited(self): 

        """ 

        This checks if the diff is already commited or broken. If it is, it 

        raises ValueError. This check is for methods that need to work only on 

        yet uncommited diffs. 

        """ 

        if self.__updater is None: 

            raise ValueError("The diff is already commited or it has raised " + 

                             "an exception, you come late") 

 

    def __data_common(self, rr, operation): 

        """ 

        Schedules an operation with rr. 

 

        It does all the real work of add_data and delete_data, including 

        all checks. 

        """ 

        self.__check_commited() 

        if rr.get_rdata_count() != 1: 

            raise ValueError('The rrset must contain exactly 1 Rdata, but ' + 

                             'it holds ' + str(rr.get_rdata_count())) 

        if rr.get_class() != self.__updater.get_class(): 

            raise ValueError("The rrset's class " + str(rr.get_class()) + 

                             " does not match updater's " + 

                             str(self.__updater.get_class())) 

        self.__buffer.append((operation, rr)) 

        if len(self.__buffer) >= DIFF_APPLY_TRESHOLD: 

            # Time to auto-apply, so the data don't accumulate too much 

            self.apply() 

 

    def add_data(self, rr): 

        """ 

        Schedules addition of an RR into the zone in this diff. 

 

        The rr is of isc.dns.RRset type and it must contain only one RR. 

        If this is not the case or if the diff was already commited, this 

        raises the ValueError exception. 

 

        The rr class must match the one of the datasource client. If 

        it does not, ValueError is raised. 

        """ 

        self.__data_common(rr, 'add') 

 

    def delete_data(self, rr): 

        """ 

        Schedules deleting an RR from the zone in this diff. 

 

        The rr is of isc.dns.RRset type and it must contain only one RR. 

        If this is not the case or if the diff was already commited, this 

        raises the ValueError exception. 

 

        The rr class must match the one of the datasource client. If 

        it does not, ValueError is raised. 

        """ 

        self.__data_common(rr, 'delete') 

 

    def compact(self): 

        """ 

        Tries to compact the operations in buffer a little by putting some of 

        the operations together, forming RRsets with more than one RR. 

 

        This is called by apply before putting the data into datasource. You 

        may, but not have to, call this manually. 

 

        Currently it merges consecutive same operations on the same 

        domain/type. We could do more fancy things, like sorting by the domain 

        and do more merging, but such diffs should be rare in practice anyway, 

        so we don't bother and do it this simple way. 

        """ 

        def same_type(rrset1, rrset2): 

            '''A helper routine to identify whether two RRsets are of the 

            same 'type'.  For RRSIGs we should consider type covered, too. 

            ''' 

            if rrset1.get_type() != isc.dns.RRType.RRSIG() or \ 

                    rrset2.get_type != isc.dns.RRType.RRSIG(): 

                return rrset1.get_type() == rrset2.get_type() 

            # RR type of the both RRsets is RRSIG.  Compare type covered. 

            # We know they have exactly one RDATA. 

            sigdata1 = rrset1.get_rdata()[0].to_text().split()[0] 

            sigdata2 = rrset2.get_rdata()[0].to_text().split()[0] 

            return sigdata1 == sigdata2 

 

        buf = [] 

        for (op, rrset) in self.__buffer: 

            old = buf[-1][1] if len(buf) > 0 else None 

            if old is None or op != buf[-1][0] or \ 

                rrset.get_name() != old.get_name() or \ 

                (not same_type(rrset, old)): 

                buf.append((op, isc.dns.RRset(rrset.get_name(), 

                                              rrset.get_class(), 

                                              rrset.get_type(), 

                                              rrset.get_ttl()))) 

            if rrset.get_ttl() != buf[-1][1].get_ttl(): 

                logger.warn(LIBXFRIN_DIFFERENT_TTL, rrset.get_ttl(), 

                            buf[-1][1].get_ttl(), rrset.get_name(), 

                            rrset.get_class(), rrset.get_type()) 

            for rdatum in rrset.get_rdata(): 

                buf[-1][1].add_rdata(rdatum) 

        self.__buffer = buf 

 

    def apply(self): 

        """ 

        Push the buffered changes inside this diff down into the data source. 

        This does not stop you from adding more changes later through this 

        diff and it does not close the datasource transaction, so the changes 

        will not be shown to others yet. It just means the internal memory 

        buffer is flushed. 

 

        This is called from time to time automatically, but you can call it 

        manually if you really want to. 

 

        This raises ValueError if the diff was already commited. 

 

        It also can raise isc.datasrc.Error. If that happens, you should stop 

        using this object and abort the modification. 

        """ 

        self.__check_commited() 

        # First, compact the data 

        self.compact() 

        try: 

            # Then pass the data inside the data source 

            for (operation, rrset) in self.__buffer: 

                if operation == 'add': 

                    self.__updater.add_rrset(rrset) 

223                elif operation == 'delete': 

                    self.__updater.delete_rrset(rrset) 

                else: 

                    raise ValueError('Unknown operation ' + operation) 

            # As everything is already in, drop the buffer 

        except: 

            # If there's a problem, we can't continue. 

            self.__updater = None 

            raise 

 

        self.__buffer = [] 

 

    def commit(self): 

        """ 

        Writes all the changes into the data source and makes them visible. 

        This closes the diff, you may not use it any more. If you try to use 

        it, you'll get ValueError. 

 

        This might raise isc.datasrc.Error. 

        """ 

        self.__check_commited() 

        # Push the data inside the data source 

        self.apply() 

        # Make sure they are visible. 

        try: 

            self.__updater.commit() 

        finally: 

            # Remove the updater. That will free some resources for one, but 

            # mark this object as already commited, so we can check 

 

            # We delete it even in case the commit failed, as that makes us 

            # unusable. 

            self.__updater = None 

 

    def get_buffer(self): 

        """ 

        Returns the current buffer of changes not yet passed into the data 

        source. It is in a form like [('add', rrset), ('delete', rrset), 

        ('delete', rrset), ...]. 

 

        Probably useful only for testing and introspection purposes. Don't 

        modify the list. 

        """ 

        return self.__buffer